//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CIISClientInterface.h"
#include "CIISCommon.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

__fastcall TCIISClientInterface::TCIISClientInterface(TCIISDBModule * SetDB,
                                                      TDebugWin * SetDebugWin,
                                                      TCIISNetInterface *SetNetInt,
                                                      TCIISServerInterface *SetSerInt,
                                                      CIISServerMode SetServerMode,
                                                      int SetPortNoLocal,
                                                      int SetPortNoPM,
                                                      int SetPortNoRemote )
{
  DB = SetDB;
  DW = SetDebugWin;
  NetInt = SetNetInt;
  SerInt = SetSerInt;
  
  ServerMode = SetServerMode;
	PortNoLocal = SetPortNoLocal;
  PortNoRemote = SetPortNoRemote;
	PortNoPM = SetPortNoPM;

  if( ServerMode == CIISRemoteServer )
    SerInt->SetTransferReply( &TransferRelpy );

    

	LSocket = new TCIIServerSocket( &GetLocalMessage, PortNoLocal, DW  );
	RSocket = new TCIIServerSocket( &GetRemoteMessage, PortNoRemote, DW  );
	PMSocket = new TCIIServerSocket( &GetPMMessage, PortNoPM, DW );

	TimerSM = new TTimer(NULL);
	TimerSM->Enabled = false;
	TimerSM->Interval = 50;
	TimerSM->OnTimer = TimerSMEvent;

  NoOpts.Clear();

	#if DebugClientInt == 1
  Debug("CIISClientInterface Created");
  #endif
}
//---------------------------------------------------------------------------
__fastcall TCIISClientInterface::~TCIISClientInterface()
{
	delete TimerSM;
	delete LSocket;
  delete RSocket;
  delete PMSocket;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::TransferRelpy( TCamurPacket *o )
{
	#if DebugClientInt == 1
	Debug( "MSG Number: " + IntToStr( o->MessageNumber ) + " to local port" + ": " + o->MessageData  );
	#endif

	if( o->MessageCommand == CIISLogin )
	{
		 if( o->MessageCode == CIISMsg_Ok ) o->MessageData = "1=" + DB->DBDir();
	}

	LSocket->PutMessage( o );
	LSocket->SendMessage();
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::GetPMMessage()
{
  while( ParsePMCommands() );
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::ParsePMCommands()
{
	TCamurPacket  *p;
  int ZoneNo;
	String Command;
	bool SM_Parse;
	TDateTime TOutOpenCom;

	SM_Parse = false;

	p = PMSocket->GetMessage();
  if( p != NULL )
	{
		PMReply = new TCamurPacket;
		PMReply->MessageType = CIISResponse;
		PMReply->PortNo = p->PortNo;
		PMReply->MessageNumber = p->MessageNumber;
		PMReply->MessageCode = CIISUnDefErr;
		PMReply->MessageCommand = p->MessageCommand;
		PMReply->MessageTable = p->MessageTable;
		PMReply->MessageData = "";

		if( p->MessageCommand == CIISLogin )
		{
			PMSocket->CancelSendMessage();
			if( PMLoggInIni()) SM_Parse = true;
			else
			{
				SM_Parse = false;
				PMReply->MessageCode = CIISMsg_LoggInError;
			}

		}
		else if( PMSocket->ClientLoggedIn )
		{
			switch( p->MessageCommand )
			{
				case CIISLogout: // Logout
					if( PMLoggOutIni()) SM_Parse = true;
					else PMReply->MessageCode = CIISUnDefErr;
					break;

				case CIISSelectProject:
					LSocket->DisconnectClient();
					if( DB->SetDBDir( p->GetArg( 1 )))
					{
						PMSocket->PrjSelected = true;
						if( ServerMode == CIISRemoteServer )
						{
							if( p->GetArg( 2 ) == "OnLine" )
							{
								if( SelectPrjIni(p->GetArg(3), p->GetArg(4)))
								{
									SM_Parse = true;
									PMReply->MessageCode = CIISMsg_Ok;
								}
								else
								{
									SM_Parse = false;
									PMReply->MessageCode = CIISMsg_LServerNotConn;
									LSocket->ConnectClient();
								}
							}
							else
							{
							DB->TabProject->Edit();
							DB->TabProject->FieldValues["ServerStatus"] = CIISDisconnected;
							DB->TabProject->Post();
							PMSocket->PrjSelOnline = false;
							LSocket->ConnectClient();
							PMReply->MessageCode = CIISMsg_Ok;
              }
						}
						else //( ServerMode == CIISLocalServer )
						{
							NetInt->ResetCanNet();
							DB->TabProject->Edit();
							DB->TabProject->FieldValues["ServerStatus"] = CIIDataSync;
							DB->TabProject->FieldValues["ServerMode"] = CIISRemoteServer;
							DB->TabProject->Post();
							LSocket->ConnectClient();
							PMReply->MessageCode = CIISMsg_Ok;
						}
					}
					break;

				case CIISReqStatus:
					if( SM_Running() || SerInt->DataSyncRunning  ) PMReply->MessageCode = CIISMsg_Busy;
					else PMReply->MessageCode = CIISMsg_Idle;
					PMReply->MessageData = SerInt->StatusMessage();
					break;

				case CIISExitProject:    //Exit project
					if( PMExitPrjIni()) SM_Parse = true;
					else
					{
						SM_Parse = false;
						PMReply->MessageCode = CIISUnDefErr;
					}
					break;

				case CIISWrite:
					if( p->MessageTable == CIISProject &&
							p->ArgInc(200) &&
							p->GetArg(200) == "SyncData" )
					{
						if( SerInt->DataSyncIni() ) PMReply->MessageCode = CIISMsg_Ok;
						else PMReply->MessageCode = CIISMsg_LServerNotConn;
					}
					else if( p->MessageTable == CIISProject &&
							p->ArgInc(200) &&
							p->GetArg(200) == "CancelSyncData" )
					{
						if( PMExitPrjIni()) SM_Parse = true;
						else
						{
							SM_Parse = false;
							PMReply->MessageCode = CIISUnDefErr;
						}
					}
					break;

			} // switch( p->MessageCommand )
		}
		else  // Not logged in
		{
			 PMReply->MessageCode = CIISMsg_UserLevelError;
			 PMReply->MessageTable = 0;
		}

		if( !SM_Parse )
		{
			PMSocket->PutMessage( PMReply );
		}

    delete p;
		return true;
  }
  else return false;
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::SelectPrjIni( String SetUserLevel, String SetPassword )
{
	if( SM_Running() ) return false;

	UserLevel = SetUserLevel;
	Password = SetPassword;
	TOutSM = Now() + 120 * OneSecond;
	SelPrjState = CIISSelPrjLoggIn;
	SelectPrjRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_SelectPrj()
{

	if( Now() > TOutSM ) SelPrjState = CIISSelPrjTimeOut;

	switch( SelPrjState )
	{
		case CIISSelPrjLoggIn :
		{
			SerInt->LoggInIni( UserLevel, Password );
			SelPrjState = CIISSelPrjCheckLoggIn;
		};
		break;

		case CIISSelPrjCheckLoggIn :
		{
			if( !SerInt->LoggInRunning )
			{
				 if( SerInt->LoggedIn )
				 {
					 PMReply->MessageCode = CIISMsg_Ok;
					 SelPrjState = CIISSelPrjSystemSync;
				 }
				 else
				 {
					 PMReply->MessageCode = CIISMsg_LoggInError;
					 SelPrjState = CIISSelPrjReady;
				 }
			}
		};
		break;

		case  CIISSelPrjSystemSync:
		{
			if( SerInt->SystemSyncIni() ) SelPrjState = CIISSelPrjCheckSync;
		};
		break;

		case  CIISSelPrjCheckSync:
		{
			if( !SerInt->SysSyncRunning ) SelPrjState = CIISSelPrjConnClient;

		};
		break;


		case  CIISSelPrjConnClient:
		{
			PMSocket->PrjSelected = true;
			PMSocket->PrjSelOnline = true;
			LSocket->ConnectClient();
			SelPrjState = CIISSelPrjReady;

		};
		break;

		case  CIISSelPrjTimeOut:
		{
			PMReply->MessageCode = CIISMsg_LoggInTimeOut;
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			SelectPrjRunning = false;
		};
		break;

		case CIISSelPrjReady :
		{
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			SelectPrjRunning = false;
		};
		break;

	}//switch( SelectPrjState );
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::PMLoggInIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 30 * OneSecond;
	PMLoggInState = CIISPMLoggInCancelSync;
	PMLoggInRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_PMLoggIn()
{
	if( Now() > TOutSM ) PMLoggInState =  CIISPMLoggInTimeOut;
	if( ServerMode != CIISRemoteServer ) PMLoggInState =  CIISPMLoggInOk;

	switch( PMLoggInState )
	{
		case CIISPMLoggInCancelSync :
		{
			if( SerInt->CancelSyncIni() ) PMLoggInState = CIISPMLoggInCheckCancelSync;
			//else PMLoggInState = CIISPMLoggInTimeOut;
		};
		break;

		case CIISPMLoggInCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )
			{
				if( SerInt->CloseComIni() ) PMLoggInState = CIISPMLoggInCheckCloseCom;
				//else PMLoggInState =  CIISPMLoggInTimeOut;
			}
		};
		break;

		case CIISPMLoggInCheckCloseCom :
		{
			if( !SerInt->CloseComRunning) PMLoggInState = CIISPMLoggInOk;
		};
		break;

		case CIISPMLoggInOk :
		{
			PMSocket->UserLevel = ULModify;
			PMSocket->ClientLoggedIn = true;

			PMReply->MessageCode = CIISMsg_Ok;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "1=" + DB->DBDir();

			PMLoggInState = CIISPMLoggInReady;
		};
		break;

		case CIISPMLoggInTimeOut :
		{
			PMSocket->UserLevel = ULNone;
			PMSocket->ClientLoggedIn = false;

			PMReply->MessageCode = CIISMsg_LoggInTimeOut;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "";
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMLoggInRunning = false;
		};
		break;

		case CIISPMLoggInReady :
		{
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMLoggInRunning = false;
		};
		break;

	 }
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::PMLoggOutIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 20 * OneSecond;
	PMLoggOutState = CIISPMLoggOutCancelSync;
	PMLoggOutRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_PMLoggOut()
{
	if( Now() > TOutSM ) PMLoggOutState =  CIISPMLoggOutTimeOut;
	if( ServerMode != CIISRemoteServer ) PMLoggOutState =  CIISPMLoggOutOk;

	switch( PMLoggOutState )
	{
		case CIISPMLoggOutCancelSync :
		{
			if( SerInt->CancelSyncIni() ) PMLoggOutState = CIISPMLoggOutCheckCancelSync;
			//else PMLoggOutState = CIISPMLoggOutTimeOut;
		};
		break;

		case CIISPMLoggOutCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )
			{
				if( SerInt->CloseComIni() ) PMLoggOutState = CIISPMLoggOutCheckCloseCom;
				//else PMLoggOutState =  CIISPMLoggOutTimeOut;
			}
		};
		break;

		case CIISPMLoggOutCheckCloseCom :
		{
			if( !SerInt->CloseComRunning) PMLoggOutState = CIISPMLoggOutOk;
		};
		break;

		case CIISPMLoggOutOk :
		{
			PMSocket->PrjSelected = false;
			PMSocket->PrjSelOnline = false;
			PMSocket->UserLevel = ULNone;
			PMSocket->ClientLoggedIn = false;

			PMReply->MessageCode = CIISMsg_Ok;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "";
			DB->Log( ClientCom, LogOut, High, "", 0, 0 );

			PMLoggOutState = CIISPMLoggOutReady;
		};
		break;

		case CIISPMLoggOutTimeOut :
		{
			PMReply->MessageCode = CIISMsg_LoggOutTimeOut;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "";
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMLoggOutRunning = false;
		};
		break;

		case CIISPMLoggOutReady :
		{
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMLoggOutRunning = false;
		};
		break;

	 }
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::PMExitPrjIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 20 * OneSecond;
	PMExitPrjState = CIISExitPrjCancelSync;
	PMExitPrjRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_PMExitPrj()
{
	if( Now() > TOutSM ) PMExitPrjState =  CIISPMExitPrjTimeOut;
	if( ServerMode != CIISRemoteServer ) PMExitPrjState =  CIISPMExitPrjOk;

	switch( PMExitPrjState )
	{
		case CIISExitPrjCancelSync :
		{
			if( SerInt->CancelSyncIni() ) PMExitPrjState = CIISExitPrjCheckCancelSync;
			//else PMExitPrjState = CIISPMExitPrjTimeOut;
		};
		break;

		case CIISExitPrjCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )
			{
				if( SerInt->CloseComIni() ) PMExitPrjState = CIISExitPrjCheckCloseCom;
				//else PMExitPrjState =  CIISPMExitPrjTimeOut;
			}
		};
		break;

		case CIISExitPrjCheckCloseCom :
		{
			if( !SerInt->CloseComRunning) PMExitPrjState = CIISPMExitPrjOk;
		};
		break;

		case CIISPMExitPrjOk :
		{
			PMSocket->PrjSelected = false;
			PMSocket->PrjSelOnline = false;
			LSocket->DisconnectClient();
			PMReply->MessageCode = CIISMsg_Ok;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "";
			DB->Log( ClientCom, LogOut, High, "", 0, 0 );

			PMExitPrjState = CIISExitPrjReady;
		};
		break;

		case CIISPMExitPrjTimeOut :
		{
			PMReply->MessageCode = CIISMsg_ExitPrjTimeOut;
			PMReply->MessageTable = 0;
			PMReply->MessageData = "";
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMExitPrjRunning = false;
		};
		break;

		case CIISExitPrjReady :
		{
			PMSocket->PutMessage( PMReply );
			PMSocket->SendMessage();
			PMExitPrjRunning = false;
		};
		break;

	 }
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::MonLoggInIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 120 * OneSecond;
	MonLoggInState = CIISMonLoggInCancelSync;
	MonLoggInRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_MonLoggIn()
{
	if( Now() > TOutSM ) MonLoggInState = CIISMonLoggInTimeOut;

	switch( MonLoggInState )
	{
		case CIISMonLoggInCancelSync :
		{
			if( SerInt->CancelSyncIni() ) MonLoggInState = CIISMonLoggInCheckCancelSync;
			//else MonLoggInState = CIISMonLoggInTimeOut;
		};
		break;

		case CIISMonLoggInCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )
			{
				if( SerInt->CloseComIni() ) MonLoggInState = CIISMonLoggInCheckCloseCom;
				//else MonLoggInState =  CIISMonLoggInTimeOut;
			}
		};
		break;

		case CIISMonLoggInCheckCloseCom :
		{
			if( !SerInt->CloseComRunning) MonLoggInState = CIISMonLoggIn;
		};
		break;

		case CIISMonLoggIn :
		{
			SerInt->LoggInIni( MonUserLevel, MonPassword );
			MonLoggInState = CIISMonCheckLoggIn;
		};
		break;

		case CIISMonCheckLoggIn :
		{
			if( !SerInt->LoggInRunning )
			{
				 if( SerInt->LoggedIn )
				 {
					 MReply->MessageCode = CIISMsg_Ok;
					 MonLoggInState = CIISMonSystemSync;
				 }
				 else MonLoggInState =  CIISMonLoggInTimeOut;

			}
		};
		break;

		case  CIISMonSystemSync:
		{
			if( SerInt->SystemSyncIni() ) MonLoggInState = CIISMonCheckSystemSync;
		};
		break;

		case  CIISMonCheckSystemSync:
		{
			if( !SerInt->SysSyncRunning ) MonLoggInState = CIISMonLoggInOk;
		};
		break;

		case CIISMonLoggInOk :
		{
			MReply->MessageCode = CIISMsg_Ok;
			MReply->MessageData = "1=" + DB->DBDir();

			MonLoggInState = CIISMonLoggInReady;
		};
		break;

		case CIISMonLoggInTimeOut :
		{
			MReply->MessageCode = CIISMsg_LoggInTimeOut;
			MReply->MessageTable = 0;
			MReply->MessageData = "";
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonLoggInRunning = false;
		};
		break;

		case CIISMonLoggInReady :
		{
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonLoggInRunning = false;
		};
		break;

	}
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::MonLoggOutIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 20 * OneSecond;
	MonLoggOutState = CIISMonLoggOutCancelSync;
	MonLoggOutRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_MonLoggOut()
{
	if( Now() > TOutSM ) MonLoggOutState =  CIISMonLoggOutTimeOut;

	switch( MonLoggOutState )
	{
		case CIISMonLoggOutCancelSync :
		{
			if( SerInt->CancelSyncIni() ) MonLoggOutState = CIISMonLoggOutCheckCancelSync;
			//else MonLoggOutState = CIISMonLoggOutTimeOut;
		};
		break;

		case CIISMonLoggOutCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )
			{
				if( SerInt->CloseComIni() ) MonLoggOutState = CIISMonLoggOutCheckCloseCom;
				//else MonLoggOutState =  CIISMonLoggOutTimeOut;
			}
		};
		break;

		case CIISMonLoggOutCheckCloseCom :
		{
			if( !SerInt->CloseComRunning) MonLoggOutState = CIISMonLoggOutOk;
		};
		break;

		case CIISMonLoggOutOk :
		{
			MReply->MessageCode = CIISMsg_Ok;
			MonLoggOutState = CIISMonLoggOutReady;
		};
		break;

		case CIISMonLoggOutTimeOut :
		{
			MReply->MessageCode = CIISMsg_LoggOutTimeOut;
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonLoggOutRunning = false;
		};
		break;

		case CIISMonLoggOutReady :
		{
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonLoggOutRunning = false;
		};
		break;
	}
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::MonCancelSyncIni()
{
	if( SM_Running() ) return false;
	TOutSM = Now() + 60 * OneSecond;
	MonCancelSyncState = CIISMonCancelSync;
	MonCancelSyncRunning = true;
	TimerSM->Enabled = true;
	return true;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SM_MonCancelSync()
{
	if( Now() > TOutSM ) MonCancelSyncState =  CIISMonCanselSyncTimeOut;

	switch( MonCancelSyncState )
	{
		case CIISMonCancelSync:
		{
			if( SerInt->CancelSyncIni() ) MonCancelSyncState = CIISMonCheckCancelSync;
			//else MonCancelSyncState = CIISMonCanselSyncTimeOut;
		};
		break;

		case CIISMonCheckCancelSync :
		{
			if( !SerInt->CancelSyncRunning )MonCancelSyncState = CIISMonCancelSyncOk;
		};
		break;

		case CIISMonCancelSyncOk :
		{
			MReply->MessageCode = CIISMsg_Ok;
			MonCancelSyncState = CIISMonCancelSyncReady;
		};
		break;

		case CIISMonCanselSyncTimeOut :
		{
			MReply->MessageCode = CIISMsg_CanSyncTimeOut;
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonCancelSyncRunning = false;
		};
		break;

		case CIISMonCancelSyncReady :
		{
			LSocket->PutMessage( MReply );
			LSocket->SendMessage();
			MonCancelSyncRunning = false;
		};
		break;
  }
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::SM_Running()
{
return ( SelectPrjRunning ||
				 PMLoggInRunning || PMLoggOutRunning  || PMExitPrjRunning ||
				 MonLoggInRunning || MonLoggOutRunning || MonCancelSyncRunning );
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::TimerSMEvent(TObject * Sender)
{
	if( SelectPrjRunning ) SM_SelectPrj();
	else if( PMLoggInRunning ) SM_PMLoggIn();
	else if( PMLoggOutRunning ) SM_PMLoggOut();
	else if( PMExitPrjRunning ) SM_PMExitPrj();
	else if( MonLoggInRunning ) SM_MonLoggIn();
	else if( MonLoggOutRunning ) SM_MonLoggOut();
	else if( MonCancelSyncRunning ) SM_MonCancelSync();
	else TimerSM->Enabled = false;

}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::GetRemoteMessage()
{
	while( ParseCommands() );
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::GetLocalMessage()
{
	while( ParseCommands() );
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::ParseCommands()
{

  TCamurPacket  *p, *o;
  int ZoneNo;
	String Command, Stop;
	bool ReadOK, ModifyOK, EraseOK;
	bool SM_Parse;
	int RNo, LPRStep, Sensor, PS;
	TDateTime RecStart, DTS;
	double  Interval;

	SM_Parse = false;
	ReadOK = false;
	ModifyOK = false;
	EraseOK = false;

	p = LSocket->GetMessage();
  if( p == NULL ) p = RSocket->GetMessage();

  if( p != NULL )
	{
		if( p->PortNo == PortNoLocal ) CurrSocket = LSocket;
		else CurrSocket = RSocket;

		if( p->PortNo == PortNoLocal && LSocket->ClientLoggedIn )
		{
			switch (LSocket->UserLevel)
			{
				case ULErase: EraseOK = true;
				case ULModify: ModifyOK = true;
				case ULReadOnly: ReadOK = true;
			}
		}
		else if( p->PortNo == PortNoRemote && RSocket->ClientLoggedIn )
		{
			switch (RSocket->UserLevel)
			{
				case ULErase: EraseOK = true;
				case ULModify: ModifyOK = true;
				case ULReadOnly: ReadOK = true;
			}
		}

    if( ServerMode == CIISRemoteServer )
		{

			#if DebugClientInt == 1
			Debug( "Parsing MSG Number: " + IntToStr( p->MessageNumber ) + ": " + p->MessageData );
			#endif

			MReply = new TCamurPacket;
			MReply->MessageType = CIISResponse;
			MReply->PortNo = p->PortNo;
			MReply->MessageNumber = p->MessageNumber;
			MReply->MessageCode = CIISUnDefErr;
			MReply->MessageCommand = p->MessageCommand;
			MReply->MessageTable = p->MessageTable;
			MReply->MessageData = "";


			if( p->MessageCommand == CIISLogin )
			{
				if( PMSocket->PrjSelected )
				{
					LSocket->UserLevel = PMSocket->UserLevel;
					LSocket->ClientLoggedIn = PMSocket->ClientLoggedIn;
					MReply->MessageData = "1=" + DB->DBDir();
					MReply->MessageCode = CIISMsg_Ok;
        }
				else
				{
					if( p->ArgInc(1) && p->ArgInc(2) )
					{
						MonUserLevel = p->GetArg(1);
						MonPassword = p->GetArg(2);

						if( MonUserLevel == "ReadOnly" && MonPassword == DB->TabProject->FieldByName("PWReadOnly")->AsString )
						{
							LSocket->UserLevel = ULReadOnly;
							LSocket->ClientLoggedIn = true;
						}
						else if( MonUserLevel == "Modify" && MonPassword == DB->TabProject->FieldByName("PWModify")->AsString )
						{
							LSocket->UserLevel = ULModify;
							LSocket->ClientLoggedIn = true;
						}
						else if( MonUserLevel == "Erase" && MonPassword == DB->TabProject->FieldByName("PWErase")->AsString )
						{
							LSocket->UserLevel = ULErase;
							LSocket->ClientLoggedIn = true;
						}
						else
						{
							LSocket->UserLevel = ULNone;
							LSocket->ClientLoggedIn = false;
						}
					}
					else  // Arg1 or Arg2 not included
					{
						LSocket->UserLevel = ULNone;
						LSocket->ClientLoggedIn = false;
					}

					if( MonLoggInIni()) SM_Parse = true;
					else MReply->MessageCode = CIISMsg_LoggInError;
				}
			}
			else if( ReadOK )
			{
				if( p->MessageCommand == CIISReqStatus )
				{
					if( SM_Running() || SerInt->DataSyncRunning ) MReply->MessageCode = CIISMsg_Busy;
					else MReply->MessageCode = CIISMsg_Idle;
					MReply->MessageData = SerInt->StatusMessage();
				}

				else if( p->MessageCommand == CIISWrite && p->MessageTable == CIISProject &&
						p->ArgInc(200) && p->GetArg(200) == "SyncData" )
				{
					if( SerInt->DataSyncIni() ) MReply->MessageCode = CIISMsg_Ok;
					else MReply->MessageCode = CIISMsg_LServerNotConn;
				}

				else if( p->MessageCommand == CIISWrite && p->MessageTable == CIISProject &&
						p->ArgInc(200) && p->GetArg(200) == "CancelSyncData" )
				{
					if( MonCancelSyncIni() ) SM_Parse = true;
					else MReply->MessageCode = CIISUnDefErr;
				}

				else if( p->MessageCommand == CIISLogout )
				{
					if( MonLoggOutIni()) SM_Parse = true;
					else MReply->MessageCode = CIISUnDefErr;
				}
				
				else
				{
					#if DebugClientInt == 1
					Debug( "Tranfering MSG Number  : "+IntToStr( p->MessageNumber ) + ": " + p->MessageData );
					#endif
					if( SerInt->TransferMessage( p ) ) SM_Parse = true;
					else MReply->MessageCode = CIISMsg_LServerNotConn;
				}
			}  // ReadOK
			else
			{
				MReply->MessageCode = CIISMsg_UserLevelError;
			}

			if( !SM_Parse )
			{
				LSocket->PutMessage( MReply );
			}
		}
    else //ServerMode == CIISLocalServer
		{
			#if DebugClientInt == 1
			Debug( "Parsing MSG Number  : "+IntToStr( p->MessageNumber ) + ": " + p->MessageData );
			#endif

			o = new TCamurPacket;
			o->MessageType = CIISResponse;
			o->PortNo = p->PortNo;
      o->MessageNumber = p->MessageNumber;
			o->MessageCode = CIISUnDefErr;
			o->MessageCommand = p->MessageCommand;
			o->MessageTable = p->MessageTable;
			o->MessageData = "";

			if( p->MessageCommand == CIISLogin )
			{
				if( p->ArgInc(1) && p->ArgInc(2) )
				{
					if( p->GetArg(1) == "ReadOnly" && p->GetArg(2) == DB->TabProject->FieldByName("PWReadOnly")->AsString )
					{
						CurrSocket->UserLevel = ULReadOnly;
						CurrSocket->ClientLoggedIn = true;
					}
					else if( p->GetArg(1) == "Modify" && p->GetArg(2) == DB->TabProject->FieldByName("PWModify")->AsString )
					{
						CurrSocket->UserLevel = ULModify;
						CurrSocket->ClientLoggedIn = true;
					}
					else if( p->GetArg(1) == "Erase" && p->GetArg(2) == DB->TabProject->FieldByName("PWErase")->AsString )
					{
						CurrSocket->UserLevel = ULErase;
						CurrSocket->ClientLoggedIn = true;
					}
					else
					{
						CurrSocket->UserLevel = ULNone;
						CurrSocket->ClientLoggedIn = false;
					}
				}
				else  // Arg1 or Arg2 not included
				{
					CurrSocket->UserLevel = ULNone;
					CurrSocket->ClientLoggedIn = false;
				}

				if( CurrSocket->ClientLoggedIn )
				{
					o->MessageCode = CIISMsg_Ok;
					o->MessageTable = 0;
					o->MessageData = "1=" + DB->DBDir();
					DB->Log( ClientCom, LogIn, High, p->GetArg(1), PortNoLocal, 0 );
				}
				else
				{
					o->MessageCode = CIISMsg_LoggInError;
					o->MessageTable = 0;
					DB->Log( ClientCom, LoggInError, High, p->GetArg(1), PortNoLocal, 0 );
				}
			} // if CIISLogin
			else if( ReadOK )    // All commands exept CIISLogin
			{
				switch( p->MessageCommand )
				{
				case CIISRead: // Read
					 switch( p->MessageTable )
					 {
					 case CIISProject: // Project
							 if( DB->TabProject->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetProjectNo" )
									 {
											o->MessageData = "2=" + DB->TabProject->FieldByName("PrjNo")->AsString + "\r\n";
											o->MessageCode = CIISMsg_Ok;
									 }
									 else if( p->GetArg( 200 ) == "GetProject" )
									 {
											o->MessageData = TabProjectRecord();
											o->MessageCode = CIISMsg_Ok;
									 }
									 else if( p->GetArg( 200 ) == "GetConfigDataVer" )
									 {
											o->MessageData = "1=" + DB->TabProject->FieldByName("ConfigVer")->AsString + "\r\n" +
																			 "2=" + DB->TabProject->FieldByName("DataVer")->AsString + "\r\n" +
																			 "3=" + DB->TabProject->FieldByName("LastValVer")->AsString + "\r\n";

											o->MessageCode = CIISMsg_Ok;
									 }
								 }
								 else
								 {
										o->MessageData =
											 "1=" + DB->TabProject->FieldByName("PrjName")->AsString + "\r\n" +
											 "2=" + DB->TabProject->FieldByName("PrjNo")->AsString + "\r\n" +
											 "3=" + DB->TabProject->FieldByName("Client")->AsString + "\r\n" +
											 "4=" + DB->TabProject->FieldByName("Consultant")->AsString + "\r\n" +
											 "5=" + DB->TabProject->FieldByName("Manager")->AsString + "\r\n";

										o->MessageCode = CIISMsg_Ok;
								 }
							 }
							 else
							 {
								 o->MessageCode = CIISMsg_RecNotFound;
							 }
							 break;

					 case CIISController: // Controller
							 if( DB->TabControllers->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetCtrl" )
									 {
										 o->MessageData = TabControllersRecord();
										 o->MessageCode = CIISMsg_Ok;
									 }
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else
							 {
								 o->MessageCode = CIISMsg_RecNotFound;
							 }
							 break;

					 case CIISZones: // Zones
							 if( DB->TabZones->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabZonesRecord();
										 o->MessageCode = CIISMsg_Ok;

										 while( o->MessageData.Length() < OutMessageLimit && DB->TabZones->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabZonesRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabZones->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabZones->Locate("ZoneNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabZones->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabZonesRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabZones->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt( p->GetArg(99));
										 if( DB->TabZones->RecordCount >= RNo)
										 {
											 DB->TabZones->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabZones->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabZonesRecord();
											 }
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabZones->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabZones->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break;

					 case CIISSensors: // Sensors
							 if( DB->TabSensors->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabSensorsRecord();
										 o->MessageCode = CIISMsg_Ok;

										 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabSensorsRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabSensors->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabSensors->Locate("SensorSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabSensorsRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabSensors->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt( p->GetArg(99));
										 if( DB->TabSensors->RecordCount >= RNo)
										 {
											 DB->TabSensors->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabSensorsRecord();
											 }
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabSensors->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }

									 else if( p->GetArg( 200 ) == "GetLastValFirstToEnd")
									 {
										 o->MessageData = TabSensorsLastValRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabSensorsLastValRecord();
										 }
									 }
									 else if( p->GetArg( 200 ) == "GetLastValNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabSensors->Locate("SensorSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabSensorsLastValRecord();
											 }
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetAlarmFirstToEnd")
									 {
										 o->MessageData = TabSensorsAlarmRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabSensorsAlarmRecord();
										 }
									 }
									 else if( p->GetArg( 200 ) == "GetAlarmNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabSensors->Locate("SensorSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabSensors->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabSensorsAlarmRecord();
											 }
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabSensors->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break;

					 case CIISPowerSupply: // PowerSupply
							 if( DB->TabPowerSupply->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabPowerSupplyRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabPowerSupplyRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabPowerSupply->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabPowerSupply->Locate("PSSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabPowerSupplyRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabPowerSupply->RecNo ) + "\r\n";
										 }
										 else  o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt( p->GetArg(99));
										 if( DB->TabPowerSupply->RecordCount >= RNo )
										 {
											 DB->TabPowerSupply->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabPowerSupplyRecord();
											 }
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabPowerSupply->RecNo ) + "\r\n";
										 }
										 else  o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetLastValFirstToEnd")
									 {
										 o->MessageData = TabPowerSupplyLastValRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabPowerSupplyLastValRecord();
										 }
									 }
									 else if( p->GetArg( 200 ) == "GetLastValNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabPowerSupply->Locate("PSSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabPowerSupplyLastValRecord();
											 }
										 }
										 else  o->MessageCode = CIISMsg_RecNotFound;
									 }

									 else if( p->GetArg( 200 ) == "GetAlarmFirstToEnd")
									 {
										 o->MessageData = TabPowerSupplyAlarmRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabPowerSupplyAlarmRecord();
										 }
									 }
									 else if( p->GetArg( 200 ) == "GetAlarmNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabPowerSupply->Locate("PSSerialNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabPowerSupply->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabPowerSupplyAlarmRecord();
											 }
										 }
										 else  o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabPowerSupply->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							break;

					 case CIISEvent: // Event
							 if( DB->TabEventLog->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabEventLogRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabEventLog->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabEventLogRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabEventLog->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1) && p->ArgInc(5) )
									 {
										 Variant Args[2];
										 Args[0] = CIISStrToDateTime(p->GetArg(1));
										 Args[1] = StrToInt(p->GetArg(5));
										 if( DB->TabEventLog->Locate("EventDateTime;EventNo", VarArrayOf( Args, 1 ), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabEventLog->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabEventLogRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabEventLog->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt(p->GetArg(99));
										 if( DB->TabEventLog->RecordCount >= RNo )
										 {
											 DB->TabEventLog->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabEventLog->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabEventLogRecord();
											 }
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabEventLog->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabEventLog->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							break;

					 case CIISRecordings:
							 if( DB->TabRecordings->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabRecordingsRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabRecordings->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabRecordingsRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabRecordings->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1) )
									 {
										 if( DB->TabRecordings->Locate("RecNo", p->GetArg(1), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabRecordings->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabRecordingsRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabRecordings->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt( p->GetArg(99));
										 if( DB->TabRecordings->RecordCount >= RNo)
										 {
											 DB->TabRecordings->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabRecordings->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabRecordingsRecord();
											 }
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabRecordings->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecStop" && p->ArgInc(1) )
									 {
										 if( DB->TabRecordings->Locate("RecNo", p->GetArg(1), NoOpts ) == true )
										 {
											 try { Stop = CIISDateTimeToStr( DB->TabRecordings->FieldByName("RecStop")->AsDateTime ); }
											 catch( ... ) { Stop = ""; }
											 o->MessageData = "";
											 o->MessageData = "1=" + Stop;
											 o->MessageCode = CIISMsg_Ok;
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabRecordings->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break; // Recordings

					 case CIISValues:
							 if( DB->TabValues->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabValuesRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabValues->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabValuesRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValues->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" &&
														p->ArgInc(1) && p->ArgInc(2) && p->ArgInc(3) && p->ArgInc(4))
									 {
										 Variant Args[4];
										 Args[0] = CIISStrToDateTime(p->GetArg(1));
										 Args[1] = StrToInt(p->GetArg(2));
										 Args[2] = StrToInt(p->GetArg(3));
										 Args[3] = p->GetArg(4);
										 if( DB->TabValues->Locate("DateTimeStamp;RecNo;SensorSerialNo;ValueType", VarArrayOf( Args, 3 ), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValues->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabValuesRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValues->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99))
									 {
										 RNo = StrToInt(p->GetArg(99));
										 if( DB->TabValues->RecordCount >= RNo )
										 {
											 DB->TabValues->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValues->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabValuesRecord();
											 }
											 if( DB->TabValues->RecNo >= DB->TabValues->RecordCount ) o->MessageData = o->MessageData + "99=-1\r\n";
											 else o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValues->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount" )
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabValues->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break; // Values

					 case CIISValuesLPR:
							 if( DB->TabValuesLPR->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabValuesLPRRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesLPR->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabValuesLPRRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesLPR->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" &&
														p->ArgInc(1) && p->ArgInc(2) && p->ArgInc(3) )
									 {
										 o->MessageCode = CIISMsg_NoLongerSupported;
                   }									 
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(1))
									 {

										 Variant Args[1];
										 Args[0] = CIISStrToDateTime(p->GetArg(1));

										 if( DB->TabValuesLPR->Locate("DateTimeStamp", VarArrayOf( Args, 0 ), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesLPR->FindNext() )
											 {
												o->MessageData = o->MessageData + TabValuesLPRRecord();
											 }
											 if( o->MessageData.Length() > 0 )
												 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesLPR->RecNo ) + "\r\n";											 
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" &&	p->ArgInc(99) )
									 {
										 RNo = StrToInt(p->GetArg(99));
										 if( DB->TabValuesLPR->RecordCount >= RNo)
										 {
											 DB->TabValuesLPR->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesLPR->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabValuesLPRRecord();
											 }
											 if( DB->TabValuesLPR->RecNo >= DB->TabValuesLPR->RecordCount ) o->MessageData = o->MessageData + "99=-1\r\n";
											 else o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesLPR->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount" )
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabValuesLPR->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break; // ValuesLPR

					 case CIISValuesDecay:
							 if( DB->TabValuesDecay->FindFirst() )
							 {
								 if( p->ArgInc( 200 ))
								 {
									 if( p->GetArg( 200 ) == "GetFirstToEnd")
									 {
										 o->MessageData = TabValuesDecayRecord();
										 o->MessageCode = CIISMsg_Ok;
										 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesDecay->FindNext() )
										 {
											 o->MessageData = o->MessageData + TabValuesDecayRecord();
										 }
										 if( o->MessageData.Length() > 0 )
											 o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesDecay->RecNo ) + "\r\n";
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" &&
														p->ArgInc(1) && p->ArgInc(2) && p->ArgInc(3) )
									 {
										 Variant Args[3];
										 Args[0] = CIISStrToDateTime(p->GetArg(1));
										 Args[1] = StrToInt(p->GetArg(2));
										 Args[2] = StrToInt(p->GetArg(3));
										 if( DB->TabValuesDecay->Locate("DateTimeStamp;RecNo;SensorSerialNo", VarArrayOf( Args, 2 ), NoOpts ) == true )
										 {
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesDecay->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabValuesDecayRecord();
											 }
											 if( o->MessageData.Length() > 0 )
											   o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesDecay->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" && p->ArgInc(99) )
									 {
										 RNo = StrToInt(p->GetArg(99));
										 if( DB->TabValuesDecay->RecordCount >= RNo)
										 {
                       DB->TabValuesDecay->RecNo = RNo;
											 o->MessageData = "";
											 o->MessageCode = CIISMsg_Ok;
											 while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesDecay->FindNext() )
											 {
												 o->MessageData = o->MessageData + TabValuesDecayRecord();
											 }
											 if( DB->TabValuesDecay->RecNo >= DB->TabValuesDecay->RecordCount ) o->MessageData = o->MessageData + "99=-1\r\n";
											 else o->MessageData = o->MessageData + "99=" + IntToStr( DB->TabValuesDecay->RecNo ) + "\r\n";
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }
									 else if( p->GetArg( 200 ) == "GetRecordCount")
									 {
										 o->MessageData = "";
										 o->MessageData = o->MessageData + "100=" + IntToStr( DB->TabValuesDecay->RecordCount ) + "\r\n";;
										 o->MessageCode = CIISMsg_Ok;
									 }
									 else o->MessageCode = CIISMsg_UnknownCommand;
								 }
							 }
							 else o->MessageCode = CIISMsg_RecNotFound;
							 break; // ValuesDecay

					 } //switch( p->MessageTable )
					 break;

				case CIISWrite: // Write
					 if( ModifyOK )
					 {
						 switch( p->MessageTable )
						 {
						 case 10: // Project
								 //if( DB->TabProject->Locate("PrjName",p->GetArg(1), NoOpts ) )
								 //{
									 DB->TabProject->Edit();
									 if( p->ArgInc(1) )
									 {
										 DB->TabProject->FieldValues["PrjName"] = p->GetArg(1);
										 DB->TabControllers->Edit();
										 DB->TabControllers->FieldValues["PrjName"] = p->GetArg(1);
										 DB->TabControllers->Post();
									 }
									 if( p->ArgInc(2) ) DB->TabProject->FieldValues["PrjNo"] = p->GetArg(2);
									 if( p->ArgInc(3) ) DB->TabProject->FieldValues["Client"] = p->GetArg(3);
									 if( p->ArgInc(4) ) DB->TabProject->FieldValues["Consultant"] = p->GetArg(4);
									 if( p->ArgInc(5) ) DB->TabProject->FieldValues["Manager"] = p->GetArg(5);
									 if( p->ArgInc(6) ) DB->TabProject->FieldValues["ContractDescription"] = p->GetArg(6);
									 if( p->ArgInc(7) ) DB->TabProject->FieldValues["MMResponsibility"] = p->GetArg(7);
									 if( p->ArgInc(8) ) DB->TabProject->FieldValues["Commissioning"] = CIISStrToDateTime(p->GetArg(8));
									 if( p->ArgInc(9) ) DB->TabProject->FieldValues["Drawings"] = p->GetArg(9);
									 if( p->ArgInc(10) ) DB->TabProject->FieldValues["Criteria"] = p->GetArg(10);
									 if( p->ArgInc(11) ) DB->TabProject->FieldValues["ConnType"] = p->GetArg(11);
									 if( p->ArgInc(12) ) DB->TabProject->FieldValues["ConnRemote"] = p->GetArg(12);
									 if( p->ArgInc(13) ) DB->TabProject->FieldValues["ConnServerIP"] = p->GetArg(13);
									 if( p->ArgInc(24) ) DB->TabProject->FieldValues["PWReadOnly"] = p->GetArg(24);
									 if( p->ArgInc(25) ) DB->TabProject->FieldValues["PWModify"] = p->GetArg(25);
									 if( p->ArgInc(26) ) DB->TabProject->FieldValues["PWErase"] = p->GetArg(26);
									 DB->TabProject->Post();
									 DB->Log( ClientCom, PrjTabChanged, High, "", 0, 0 );
									 o->MessageCode = CIISMsg_Ok;
								 //}
								 //else
								 //{
								 //  o->MessageCode = CIISMsg_RecNotFound;
								 //}
								 break;
						 case 20: // Controller
								 if( DB->TabControllers->Locate("CtrlName",p->GetArg(1), NoOpts ) )
								 {

									 if( p->ArgInc(200) )
									 {
										 if( p->GetArg(200) == "Reset CANNet" ) NetInt->ResetCanNet();
										 if( p->GetArg(200) == "Update LastValue" ) NetInt->UpdateLastValue( 0, 0 );
									 }
									 else
									 {
										 DB->TabControllers->Edit();
										 if( p->ArgInc(1) ) DB->TabControllers->FieldValues["CtrlName"] = p->GetArg(1);

										 if( p->ArgInc(2) )
										 {
											 if( p->GetArg(2) == "True" ) DB->TabControllers->FieldValues["NoIR"] = true;
											 else DB->TabControllers->FieldValues["NoIR"] = false;
										 } ;
										 if( p->ArgInc(3) )
										 {
											 DB->TabControllers->FieldValues["ScheduleDateTime"] = CIISStrToDateTime(p->GetArg(3));
											 if( CIISStrToDateTime(p->GetArg(3)) > Now() ) DB->TabControllers->FieldValues["CtrlScheduleStatus"] = 1;
											 else DB->TabControllers->FieldValues["CtrlScheduleStatus"] = 0;
										 }
										 if( p->ArgInc(4) ) DB->TabControllers->FieldValues["SchedulePeriod"] = StrToInt(p->GetArg(4));
										 if( p->ArgInc(5) ) DB->TabControllers->FieldValues["DecaySampInterval"] = StrToInt(p->GetArg(5));
										 if( p->ArgInc(6) ) DB->TabControllers->FieldValues["DecayDuration"] = StrToInt(p->GetArg(6));
										 if( p->ArgInc(7) ) DB->TabControllers->FieldValues["LPRRange"] = StrToInt(p->GetArg(7));
										 if( p->ArgInc(8) ) DB->TabControllers->FieldValues["LPRStep"] = StrToInt(p->GetArg(8));
										 if( p->ArgInc(9) ) DB->TabControllers->FieldValues["LPRDelay1"] = StrToInt(p->GetArg(9));
										 if( p->ArgInc(10) ) DB->TabControllers->FieldValues["LPRDelay2"] = StrToInt(p->GetArg(10));
										 if( p->ArgInc(11) ) DB->TabControllers->FieldValues["SampInterval1"] = StrToInt(p->GetArg(11));
										 if( p->ArgInc(12) ) DB->TabControllers->FieldValues["SampInterval2"] = StrToInt(p->GetArg(12));
										 if( p->ArgInc(13) ) DB->TabControllers->FieldValues["SampInterval3"] = StrToInt(p->GetArg(13));
										 if( p->ArgInc(14) ) DB->TabControllers->FieldValues["SampInterval4"] = StrToInt(p->GetArg(14));
										 if( p->ArgInc(15) ) DB->TabControllers->FieldValues["SampInterval5"] = StrToInt(p->GetArg(15));
										 if( p->ArgInc(16) ) DB->TabControllers->FieldValues["DefaultSensorLow"] = CIISStrToFloat(p->GetArg(16));
										 if( p->ArgInc(17) ) DB->TabControllers->FieldValues["DefaultSensorHigh"] = CIISStrToFloat(p->GetArg(17));
										 if( p->ArgInc(18) ) DB->TabControllers->FieldValues["DefaultAlarmEnable"] = p->GetArg(18);
										 if( p->ArgInc(19) ) DB->TabControllers->FieldValues["SchedulePeriodUnit"] = StrToInt(p->GetArg(19));
										 if( p->ArgInc(20) ) DB->TabControllers->FieldValues["ScheduleDecay"] = p->GetArg(20);
										 if( p->ArgInc(21) ) DB->TabControllers->FieldValues["ScheduleLPR"] = p->GetArg(21);
										 if( p->ArgInc(22) ) DB->TabControllers->FieldValues["LPRMode"] = p->GetArg(22);
										 if( p->ArgInc(50) )
										 {
											 DB->TabControllers->FieldValues["PrjName"] = p->GetArg(50);
											 DB->TabProject->Edit();
											 DB->TabProject->FieldValues["PrjName"] = p->GetArg(50);
											 DB->TabProject->Post();
										 }
										 DB->TabControllers->Post();

										 DB->Log( ClientCom, CtrlTabChanged, High, "", 0, 0 );

										 // Actions
										 if( p->ArgInc(3) ) NetInt->UpdateScheduleDateTime();


									 }
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 30: // Sensors
								 Sensor =  StrToInt(p->GetArg(1));
								 if( DB->TabSensors->Locate("SensorSerialNo", Sensor, NoOpts ) )
								 {
									 if( p->ArgInc(200) )
									 {
										 if( p->GetArg(200) == "Update LastValue" ) NetInt->UpdateLastValue( 0, StrToInt(p->GetArg(1)) );
									 }
									 else
									 {
										 DB->TabSensors->Edit();
										 if( p->ArgInc(2) ) DB->TabSensors->FieldValues["SensorName"] = p->GetArg(2);
										 if( p->ArgInc(3) ) DB->TabSensors->FieldValues["PreCommOff"] = CIISStrToFloat(p->GetArg(3));
										 if( p->ArgInc(4) ) DB->TabSensors->FieldValues["PreCommLPR"] = CIISStrToFloat(p->GetArg(4));
										 if( p->ArgInc(5) ) DB->TabSensors->FieldValues["SensorArea"] = CIISStrToFloat(p->GetArg(5));
										 if( p->ArgInc(6) ) DB->TabSensors->FieldValues["SensorGain"] = CIISStrToFloat(p->GetArg(6));
										 if( p->ArgInc(7) ) DB->TabSensors->FieldValues["SensorOffset"] = CIISStrToFloat(p->GetArg(7));
										 if( p->ArgInc(8) ) DB->TabSensors->FieldValues["SensorLow"] = CIISStrToFloat(p->GetArg(8));
										 if( p->ArgInc(9) ) DB->TabSensors->FieldValues["SensorHigh"] = CIISStrToFloat(p->GetArg(9));
										 if( p->ArgInc(10) ) DB->TabSensors->FieldValues["SensorXPos"] = CIISStrToFloat(p->GetArg(10));
										 if( p->ArgInc(11) ) DB->TabSensors->FieldValues["SensorYPos"] = CIISStrToFloat(p->GetArg(11));
										 if( p->ArgInc(12) ) DB->TabSensors->FieldValues["SensorZPos"] = CIISStrToFloat(p->GetArg(12));
										 if( p->ArgInc(13) )
											 if( p->GetArg(13) == "True" )
											 {
												 DB->TabSensors->FieldValues["SensorAlarmEnabled"] = true;
												 DB->Log( ClientCom, AlarmEnable, High, p->GetArg(1), 0, 0 );
											 }
											 else
											 {
												 DB->TabSensors->FieldValues["SensorAlarmEnabled"] = false;
												 DB->TabSensors->FieldValues["SensorAlarmStatus"] = 0;
												 DB->Log( ClientCom, AlarmDisable, High, p->GetArg(1), 0, 0 );
												 
											 }
										 if( p->ArgInc(14) ) DB->TabSensors->FieldValues["SensorSectionNo"] = StrToInt(p->GetArg(14));
										 if( p->ArgInc(15) ) DB->TabSensors->FieldValues["SensorUnit"] = p->GetArg(15);
										 if( p->ArgInc(16) ) DB->TabSensors->FieldValues["SensorWarmUp"] = StrToInt(p->GetArg(16));
										 if( p->ArgInc(17) ) DB->TabSensors->FieldValues["SensorTemp"] = p->GetArg(17);
										 if( p->ArgInc(50) ) DB->TabSensors->FieldValues["ZoneNo"] = StrToInt(p->GetArg(50));
										 DB->TabSensors->Post();
										 NetInt->UpdateAlarms();
										 // Actions
										 if( p->ArgInc(50) )
										 {
											 NetInt->MoveSensor(Sensor);
											 DB->Log( ClientCom, SensorMoved, High, p->GetArg(1), 0, 0 );
										 }
										 DB->Log( ClientCom, SensorTabChanged, High, "", 0, 0 );
									 }
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 40: // Zones
								 if( DB->TabZones->Locate("ZoneNo",StrToInt(p->GetArg(1)), NoOpts ) )
								 {
									 if( p->ArgInc(200) )
									 {
										 Command = p->GetArg(200);
										 ZoneNo = StrToInt( p->GetArg(1) );
										 if( ( Command == "Stop" ) || ( Command == "0" ) )
										 {
											 if( !NetInt->StopRecording( ZoneNo )) o->MessageCode = 206;
										 }
										 else if( ( Command == "Start Monitor" ) || ( Command == "1" ) )
										 {
											 if( !NetInt->StartMonitor( ZoneNo, false )) o->MessageCode = 205;
										 }
										 else if( ( Command == "Start LPR" ) || ( Command == "2" ))
										 {
											 if( !NetInt->StartLPR( ZoneNo )) o->MessageCode = 205;
										 }
										 else if( ( Command == "Start Decay" ) || ( Command == "3" ) )
										 {
											 if( !NetInt->StartDecay( ZoneNo )) o->MessageCode = 205;
										 }
										 else if( Command == "Update LastValue" ) NetInt->UpdateLastValue( ZoneNo, 0 );
									 }  // ArgInc(200)
									 else
									 {
										 DB->TabZones->Edit();
										 if( p->ArgInc(2) ) DB->TabZones->FieldValues["ZoneName"] = p->GetArg(2);
										 if( p->ArgInc(3) ) DB->TabZones->FieldValues["AnodeArea"] = CIISStrToFloat(p->GetArg(3));
										 if( p->ArgInc(4) ) DB->TabZones->FieldValues["CathodeArea"] = CIISStrToFloat(p->GetArg(4));
										 if( p->ArgInc(5) ) DB->TabZones->FieldValues["Comment"] = p->GetArg(5);
										 if( p->ArgInc(6) ) DB->TabZones->FieldValues["ZoneSampInterval"] = StrToInt(p->GetArg(6));
										 if( p->ArgInc(50) ) DB->TabZones->FieldValues["CtrlName"] = p->GetArg(50);
										 DB->Log( ClientCom, ZoneTabChanged, High, "", 0, 0 );
										 DB->TabZones->Post();
										 DB->Log( ClientCom, ZoneTabChanged, High, "", 0, 0 );
									 }
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 50: // Values
								 o->MessageCode = 102;
								 break;
						 case 60: // PowerSupply
								 PS =  StrToInt(p->GetArg(1));
								 if( DB->TabPowerSupply->Locate("PSSerialNo", PS, NoOpts ) )
								 {
									 if( p->ArgInc(200) )
									 {
										 if( p->GetArg(200) == "Update LastValue" ) NetInt->UpdateLastValue( 0, StrToInt(p->GetArg(1)) );
									 }
									 else
									 {
										 DB->TabPowerSupply->Edit();
										 if( p->ArgInc(2) ) DB->TabPowerSupply->FieldValues["PSName"] = p->GetArg(2);
										 if( p->ArgInc(3) ) DB->TabPowerSupply->FieldValues["PSSerialID"] = p->GetArg(3);
										 if( p->ArgInc(4) ) DB->TabPowerSupply->FieldValues["PSType"] = StrToInt(p->GetArg(4));
										 if( p->ArgInc(5) ) DB->TabPowerSupply->FieldValues["PSMode"] = StrToInt(p->GetArg(5));
										 if( p->ArgInc(6) ) DB->TabPowerSupply->FieldValues["PSSetVoltage"] = CIISStrToFloat(p->GetArg(6));
										 if( p->ArgInc(7) ) DB->TabPowerSupply->FieldValues["PSSetCurrent"] = CIISStrToFloat(p->GetArg(7));
										 if( p->ArgInc(8) ) DB->TabPowerSupply->FieldValues["PSOutGain"] = CIISStrToFloat(p->GetArg(8));
										 if( p->ArgInc(9) ) DB->TabPowerSupply->FieldValues["PSOutOffset"] = CIISStrToFloat(p->GetArg(9));
										 if( p->ArgInc(10) ) DB->TabPowerSupply->FieldValues["PSLowU"] = CIISStrToFloat(p->GetArg(10));
										 if( p->ArgInc(11) ) DB->TabPowerSupply->FieldValues["PSLowI"] = CIISStrToFloat(p->GetArg(11));
										 if( p->ArgInc(12) ) DB->TabPowerSupply->FieldValues["PSHighU"] = CIISStrToFloat(p->GetArg(12));
										 if( p->ArgInc(13) ) DB->TabPowerSupply->FieldValues["PSHighI"] = CIISStrToFloat(p->GetArg(13));
										 if( p->ArgInc(14) )
											 if( p->GetArg(14) == "True" )
											 {
												 DB->TabPowerSupply->FieldValues["PSAlarmEnabled"] = true;
												 DB->Log( ClientCom, AlarmEnable, High, p->GetArg(1), 0, 0 );
											 }
											 else
											 {
												 DB->TabPowerSupply->FieldValues["PSAlarmEnabled"] = false;
												 DB->TabPowerSupply->FieldValues["PSAlarmStatusU"] = 0;
												 DB->TabPowerSupply->FieldValues["PSAlarmStatusI"] = 0;
												 DB->Log( ClientCom, AlarmDisable, High, p->GetArg(1), 0, 0 );

											 }
										 if( p->ArgInc(15) )
										 {
											 if( p->GetArg(15) == "True" )
											 {
												 DB->TabPowerSupply->FieldValues["PSVOutEnabled"] = true;
												 DB->Log( ClientCom, PSVOutEnable, High, p->GetArg(1), 0, 0 );
											 }
											 else
											 {
												 DB->TabPowerSupply->FieldValues["PSVOutEnabled"] = false;
												 DB->Log( ClientCom, PSVOutDisable, High, p->GetArg(1), 0, 0 );
											 }
										 }
										 if( p->ArgInc(16) )
										 {
											 if( p->GetArg(16) == "True" )
											 {
												 DB->TabPowerSupply->FieldValues["PSRemote"] = true;
												 DB->Log( ClientCom, PSRemote, High, p->GetArg(1), 0, 0 );
											 }
											 else
											 {
												 DB->TabPowerSupply->FieldValues["PSRemote"] = false;
												 DB->Log( ClientCom, PSLocal, High, p->GetArg(1), 0, 0 );
											 }
										 }
										 if( p->ArgInc(17) ) DB->TabPowerSupply->FieldValues["Ch1Gain"] = CIISStrToFloat(p->GetArg(17));
										 if( p->ArgInc(18) ) DB->TabPowerSupply->FieldValues["Ch1Offset"] = CIISStrToFloat(p->GetArg(18));
										 if( p->ArgInc(19) ) DB->TabPowerSupply->FieldValues["Ch1Unit"] = p->GetArg(19);
										 if( p->ArgInc(20) ) DB->TabPowerSupply->FieldValues["Ch2Gain"] = CIISStrToFloat(p->GetArg(20));
										 if( p->ArgInc(21) ) DB->TabPowerSupply->FieldValues["Ch2Offset"] = CIISStrToFloat(p->GetArg(21));
										 if( p->ArgInc(22) ) DB->TabPowerSupply->FieldValues["Ch2Unit"] = p->GetArg(22);
										 if( p->ArgInc(23) ) DB->TabPowerSupply->FieldValues["Ch3Gain"] = CIISStrToFloat(p->GetArg(23));
										 if( p->ArgInc(24) ) DB->TabPowerSupply->FieldValues["Ch3Offset"] = CIISStrToFloat(p->GetArg(24));
										 if( p->ArgInc(25) ) DB->TabPowerSupply->FieldValues["Ch3Unit"] = p->GetArg(25);
										 if( p->ArgInc(26) ) DB->TabPowerSupply->FieldValues["Ch4Gain"] = CIISStrToFloat(p->GetArg(26));
										 if( p->ArgInc(27) ) DB->TabPowerSupply->FieldValues["Ch4Offset"] = CIISStrToFloat(p->GetArg(27));
										 if( p->ArgInc(28) ) DB->TabPowerSupply->FieldValues["Ch4Unit"] = p->GetArg(28);
										 if( p->ArgInc(29) ) DB->TabPowerSupply->FieldValues["Ch1BitVal"] = CIISStrToFloat(p->GetArg(29));
										 if( p->ArgInc(30) ) DB->TabPowerSupply->FieldValues["Ch2BitVal"] = CIISStrToFloat(p->GetArg(30));
										 if( p->ArgInc(31) ) DB->TabPowerSupply->FieldValues["Ch3BitVal"] = CIISStrToFloat(p->GetArg(31));
										 if( p->ArgInc(32) ) DB->TabPowerSupply->FieldValues["Ch4BitVal"] = CIISStrToFloat(p->GetArg(32));
										 if( p->ArgInc(50) ) DB->TabPowerSupply->FieldValues["ZoneNo"] = StrToInt(p->GetArg(50));
										 DB->TabPowerSupply->Post();
										 NetInt->UpdateAlarms();
										 DB->Log( ClientCom, PSTabChanged, High, "", 0, 0 );

										 // Actions

										 if( p->ArgInc(50) )
										 {
											 NetInt->MovePS(PS);
											 DB->Log( ClientCom, PSMoved, High, p->GetArg(1), 0, 0 );
										 }


										 if( p->ArgInc(14) ) NetInt->UpdateAlarms();

										 if( p->ArgInc(5) || p->ArgInc(6) || p->ArgInc(7) || p->ArgInc(8) || p->ArgInc(9) ||
												p->ArgInc(15) || p->ArgInc(16) )
										 {
											 if( DB->TabPowerSupply->Locate("PSSerialNo",StrToInt(p->GetArg(1)), NoOpts ) )
											 {
												 NetInt->UpdateCurrentPS();
											 }
										 }
									 }
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
							} //switch( p->MessageTable )
           } // ModifyOK
					 else
					 {
						 o->MessageCode = CIISMsg_UserLevelError;
					 }
					 break;

				case 12: // Append
					 if( ModifyOK )
					 {
						 switch( p->MessageTable )
						 {
						 case 10: // Project
								 if( DB->TabProject->RecordCount == 0 )
								 {
									 DB->TabProject->Append();
									 if( p->ArgInc(1) ) DB->TabProject->FieldValues["PrjName"] = p->GetArg(1);
									 if( p->ArgInc(2) ) DB->TabProject->FieldValues["PrjNo"] = p->GetArg(2);
									 if( p->ArgInc(3) ) DB->TabProject->FieldValues["Client"] = p->GetArg(3);
									 if( p->ArgInc(4) ) DB->TabProject->FieldValues["Consultant"] = p->GetArg(4);
									 if( p->ArgInc(5) ) DB->TabProject->FieldValues["Manager"] = p->GetArg(5);
									 if( p->ArgInc(6) ) DB->TabProject->FieldValues["ContractDescription"] = p->GetArg(6);
									 if( p->ArgInc(7) ) DB->TabProject->FieldValues["MMResponsibility"] = p->GetArg(7);
									 if( p->ArgInc(8) ) DB->TabProject->FieldValues["Commissioning"] = CIISStrToDateTime( p->GetArg(8));
									 if( p->ArgInc(9) ) DB->TabProject->FieldValues["Drawings"] = p->GetArg(9);
									 if( p->ArgInc(10) ) DB->TabProject->FieldValues["Criteria"] = p->GetArg(10);
									 if( p->ArgInc(11) ) DB->TabProject->FieldValues["ConnType"] = p->GetArg(11);
									 if( p->ArgInc(12) ) DB->TabProject->FieldValues["ConnRemote"] = p->GetArg(12);
									 if( p->ArgInc(13) ) DB->TabProject->FieldValues["ConnServerIP"] = p->GetArg(13);
									 DB->TabProject->Post();
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = 101;
								 }
								 break;
						 case 20: // Controller
								 DB->TabControllers->Append();
								 if( p->ArgInc(1) ) DB->TabControllers->FieldValues["CtrlName"] = p->GetArg(1);
								 if( p->ArgInc(2) )
								 {
									 if( p->GetArg(2) == "True" ) DB->TabControllers->FieldValues["NoIR"] = true;
									 else DB->TabControllers->FieldValues["NoIR"] = false;
								 }
								 if( p->ArgInc(3) )
								 {
									 DB->TabControllers->FieldValues["ScheduleDateTime"] = CIISStrToDateTime(p->GetArg(3));
									 if( CIISStrToDateTime(p->GetArg(3)) > Now() ) DB->TabControllers->FieldValues["CtrlScheduleStatus"] = 1;
									 else DB->TabControllers->FieldValues["CtrlScheduleStatus"] = 0;
								 }
								 if( p->ArgInc(4) ) DB->TabControllers->FieldValues["SchedulePeriod"] = StrToInt(p->GetArg(4));
								 if( p->ArgInc(5) ) DB->TabControllers->FieldValues["DecaySampInterval"] = StrToInt(p->GetArg(5));
								 if( p->ArgInc(6) ) DB->TabControllers->FieldValues["DecayDuration"] = StrToInt(p->GetArg(6));
								 if( p->ArgInc(7) ) DB->TabControllers->FieldValues["LPRRange"] = StrToInt(p->GetArg(7));
								 if( p->ArgInc(8) ) DB->TabControllers->FieldValues["LPRStep"] = StrToInt(p->GetArg(8));
								 if( p->ArgInc(9) ) DB->TabControllers->FieldValues["LPRDelay1"] = StrToInt(p->GetArg(9));
								 if( p->ArgInc(10) ) DB->TabControllers->FieldValues["LPRDelay2"] = StrToInt(p->GetArg(10));
								 if( p->ArgInc(11) ) DB->TabControllers->FieldValues["SampInterval1"] = StrToInt(p->GetArg(11));
								 if( p->ArgInc(12) ) DB->TabControllers->FieldValues["SampInterval2"] = StrToInt(p->GetArg(12));
								 if( p->ArgInc(13) ) DB->TabControllers->FieldValues["SampInterval3"] = StrToInt(p->GetArg(13));
								 if( p->ArgInc(14) ) DB->TabControllers->FieldValues["SampInterval4"] = StrToInt(p->GetArg(14));
								 if( p->ArgInc(15) ) DB->TabControllers->FieldValues["SampInterval5"] = StrToInt(p->GetArg(15));
								 if( p->ArgInc(16) ) DB->TabControllers->FieldValues["DefaultSensorLow"] = CIISStrToFloat(p->GetArg(16));
								 if( p->ArgInc(17) ) DB->TabControllers->FieldValues["DefaultSensorHigh"] = CIISStrToFloat(p->GetArg(17));
								 if( p->ArgInc(18) ) DB->TabControllers->FieldValues["DefaultAlarmEnable"] = p->GetArg(18);
								 if( p->ArgInc(19) ) DB->TabControllers->FieldValues["SchedulePeriodUnit"] = StrToInt(p->GetArg(19));
								 if( p->ArgInc(20) ) DB->TabControllers->FieldValues["ScheduleDecay"] = p->GetArg(20);
								 if( p->ArgInc(21) ) DB->TabControllers->FieldValues["ScheduleLPR"] = p->GetArg(21);
								 if( p->ArgInc(22) ) DB->TabControllers->FieldValues["LPRMode"] = p->GetArg(22);
								 if( p->ArgInc(50) ) DB->TabControllers->FieldValues["PrjName"] = p->GetArg(50);
								 DB->TabControllers->Post();

								 // Actions
								 if( p->ArgInc(3) ) NetInt->UpdateScheduleDateTime();

								 o->MessageCode = CIISMsg_Ok;

								 break;
						 case 30: // Sensors
								 DB->TabSensors->Append();

								 DB->TabSensors->FieldValues["SensorStatus"] = 0;
								 DB->TabSensors->FieldValues["ZoneNo"] = 1;
								 DB->TabSensors->FieldValues["SensorType"] = 0;
								 DB->TabSensors->FieldValues["RequestStatus"] = -1;
								 DB->TabSensors->FieldValues["SensorLPRStep"] = 0;
								 DB->TabSensors->FieldValues["SensorAlarmEnabled"] = false;
								 DB->TabSensors->FieldValues["SensorAlarmStatus"] = 0;
								 DB->TabSensors->FieldValues["SensorLow"] = 0;
								 DB->TabSensors->FieldValues["SensorHigh"] = 0;
								 DB->TabSensors->FieldValues["SensorGain"] = 1;
								 DB->TabSensors->FieldValues["SensorOffset"] = 0;
								 DB->TabSensors->FieldValues["SensorUnit"] = "mV";
								 DB->TabSensors->FieldValues["SensorConnected"] = false;
								 DB->TabSensors->FieldValues["SensorWarmUp"] = 0;
								 DB->TabSensors->FieldValues["SensorTemp"] = false;
								 DB->TabSensors->FieldValues["SensorVerMajor"] = 0;
								 DB->TabSensors->FieldValues["SensorVerMinor"] = 0;

								 if( p->ArgInc(1) ) DB->TabSensors->FieldValues["SensorSerialNo"] = StrToInt(p->GetArg(1));
								 if( p->ArgInc(2) ) DB->TabSensors->FieldValues["SensorCanAdr"] = StrToInt(p->GetArg(2));
								 if( p->ArgInc(3) ) DB->TabSensors->FieldValues["SensorName"] = p->GetArg(3);
								 if( p->ArgInc(4) ) DB->TabSensors->FieldValues["Type"] = StrToInt(p->GetArg(4));
								 if( p->ArgInc(5) ) DB->TabSensors->FieldValues["SensorDMM"] = CIISStrToFloat(p->GetArg(5));
								 if( p->ArgInc(6) ) DB->TabSensors->FieldValues["PreCommOff"] = CIISStrToFloat(p->GetArg(6));
								 if( p->ArgInc(7) ) DB->TabSensors->FieldValues["PreCommLPR"] = CIISStrToFloat(p->GetArg(7));
								 if( p->ArgInc(8) ) DB->TabSensors->FieldValues["SensorArea"] = CIISStrToFloat(p->GetArg(8));
								 if( p->ArgInc(9) ) DB->TabSensors->FieldValues["SensorGain"] = CIISStrToFloat(p->GetArg(9));
								 if( p->ArgInc(10) ) DB->TabSensors->FieldValues["SensorOffset"] = CIISStrToFloat(p->GetArg(10));
								 if( p->ArgInc(11) ) DB->TabSensors->FieldValues["SensorLow"] = CIISStrToFloat(p->GetArg(11));
								 if( p->ArgInc(12) ) DB->TabSensors->FieldValues["SensorHigh"] = CIISStrToFloat(p->GetArg(12));
								 if( p->ArgInc(13) ) DB->TabSensors->FieldValues["SensorXPos"] = CIISStrToFloat(p->GetArg(13));
								 if( p->ArgInc(14) ) DB->TabSensors->FieldValues["SensorYPos"] = CIISStrToFloat(p->GetArg(14));
								 if( p->ArgInc(15) ) DB->TabSensors->FieldValues["SensorZPos"] = CIISStrToFloat(p->GetArg(15));
								 if( p->ArgInc(16) ) DB->TabSensors->FieldValues["SensorWarmUp"] = StrToInt(p->GetArg(16));
								 if( p->ArgInc(17) ) DB->TabSensors->FieldValues["SensorTemp"] = p->GetArg(17);
								 if( p->ArgInc(50) ) DB->TabSensors->FieldValues["ZoneNo"] = StrToInt(p->GetArg(50));
								 DB->TabSensors->Post();
								 o->MessageCode = CIISMsg_Ok;
								 break;
						 case 40: // Zones
								 DB->TabZones->FindLast();
					 ZoneNo = DB->TabZones->FieldByName("ZoneNo")->AsInteger + 1;
								 DB->TabZones->Append();
								 DB->TabZones->FieldValues["ZoneNo"] = ZoneNo;
								 DB->TabZones->FieldValues["RecType"] = 0;
								 DB->TabZones->FieldValues["RecNo"] = 0;
								 DB->TabZones->FieldValues["ZoneSampInterval"] = 1;
								 DB->TabZones->FieldValues["ZoneAlarmStatus"] = 0;
								 DB->TabZones->FieldValues["ZoneScheduleStatus"] = 0;
								 DB->TabZones->FieldValues["ZoneCanAdr"] = NetInt->GetCanAdr();
								 if( p->ArgInc(2) ) DB->TabZones->FieldValues["ZoneName"] = p->GetArg(2);
								 if( p->ArgInc(3) ) DB->TabZones->FieldValues["AnodeArea"] = CIISStrToFloat(p->GetArg(3));
								 if( p->ArgInc(4) ) DB->TabZones->FieldValues["CathodeArea"] = CIISStrToFloat(p->GetArg(4));
								 if( p->ArgInc(5) ) DB->TabZones->FieldValues["Comment"] = p->GetArg(5);
								 if( p->ArgInc(6) ) DB->TabZones->FieldValues["ZoneSampInterval"] = StrToInt(p->GetArg(6));
								 if( p->ArgInc(7) ) DB->TabZones->FieldValues["ZoneScheduleStatus"] = StrToInt(p->GetArg(7));
								 if( p->ArgInc(50) ) DB->TabZones->FieldValues["CtrlName"] = p->GetArg(50);
								 DB->Log( ClientCom, ZoneTabChanged, High, "", 0, 0 );
								 DB->TabZones->Post();
								 o->MessageCode = CIISMsg_Ok;
								 break;
						 case 50: // Values
								 break;
						 case 60: // PowerSupply
								 DB->TabPowerSupply->Append();

								 DB->TabPowerSupply->FieldValues["PSStatus"] = 0;
								 DB->TabPowerSupply->FieldValues["PSType"] = 11;
								 DB->TabPowerSupply->FieldValues["RequestStatus"] = -1;
								 DB->TabPowerSupply->FieldValues["PSAlarmEnabled"] = False;
								 DB->TabPowerSupply->FieldValues["PSAlarmStatusU"] = 0;
								 DB->TabPowerSupply->FieldValues["PSAlarmStatusI"] = 0;
								 DB->TabPowerSupply->FieldValues["PSLowU"] = 0;
								 DB->TabPowerSupply->FieldValues["PSHighU"] = 0;
								 DB->TabPowerSupply->FieldValues["PSLowI"] = 0;
								 DB->TabPowerSupply->FieldValues["PSHighI"] = 0;
								 DB->TabPowerSupply->FieldValues["PSMode"] = 0;
								 DB->TabPowerSupply->FieldValues["PSMode"] = 0;
								 DB->TabPowerSupply->FieldValues["PSSetVoltage"] = 0;
								 DB->TabPowerSupply->FieldValues["PSSetCurrent"] = 0;
								 DB->TabPowerSupply->FieldValues["PSOutGain"] = 1;
								 DB->TabPowerSupply->FieldValues["PSOutOffset"] = 0;
								 DB->TabPowerSupply->FieldValues["PSVOutEnabled"] = false;
								 DB->TabPowerSupply->FieldValues["PSRemote"] = false;
								 DB->TabPowerSupply->FieldValues["PSConnected"] = false;
								 DB->TabPowerSupply->FieldValues["Ch1Gain"] = 1;
								 DB->TabPowerSupply->FieldValues["Ch1Offset"] = 0;
								 DB->TabPowerSupply->FieldValues["Ch1Unit"] = "V";
								 DB->TabPowerSupply->FieldValues["Ch2Gain"] = 1;
								 DB->TabPowerSupply->FieldValues["Ch2Offset"] = 0;
								 DB->TabPowerSupply->FieldValues["Ch2Unit"] = "A";
								 DB->TabPowerSupply->FieldValues["Ch3Gain"] = 1;
								 DB->TabPowerSupply->FieldValues["Ch3Offset"] = 0;
								 DB->TabPowerSupply->FieldValues["Ch3Unit"] = "?";
								 DB->TabPowerSupply->FieldValues["Ch4Gain"] = 1;
								 DB->TabPowerSupply->FieldValues["Ch4Offset"] = 0;
								 DB->TabPowerSupply->FieldValues["Ch4Unit"] = "?";
								 DB->TabPowerSupply->FieldValues["Ch1BitVal"] = 5.0/32767;
								 DB->TabPowerSupply->FieldValues["Ch2BitVal"] = 5.0/32767;
								 DB->TabPowerSupply->FieldValues["Ch3BitVal"] = 5.0/32767;
								 DB->TabPowerSupply->FieldValues["Ch4BitVal"] = 5.0/32767;
								 DB->TabPowerSupply->FieldValues["PSVerMajor"] = 0;
								 DB->TabPowerSupply->FieldValues["PSVerMinor"] = 0;

								 if( p->ArgInc(1) ) DB->TabPowerSupply->FieldValues["PSSerialNo"] = StrToInt(p->GetArg(1));
								 if( p->ArgInc(2) ) DB->TabPowerSupply->FieldValues["PSName"] = p->GetArg(2);
								 if( p->ArgInc(3) ) DB->TabPowerSupply->FieldValues["PSSerialNo"] = p->GetArg(3);
								 if( p->ArgInc(4) ) DB->TabPowerSupply->FieldValues["PSType"] = StrToInt(p->GetArg(4));
								 if( p->ArgInc(5) ) DB->TabPowerSupply->FieldValues["PSMode"] = StrToInt(p->GetArg(5));
								 if( p->ArgInc(6) ) DB->TabPowerSupply->FieldValues["PSSetVoltage"] = CIISStrToFloat(p->GetArg(6));
								 if( p->ArgInc(7) ) DB->TabPowerSupply->FieldValues["PSSetCurrent"] = CIISStrToFloat(p->GetArg(7));
								 if( p->ArgInc(8) ) DB->TabPowerSupply->FieldValues["PSOutGain"] = CIISStrToFloat(p->GetArg(8));
								 if( p->ArgInc(9) ) DB->TabPowerSupply->FieldValues["PSOutOffset"] = CIISStrToFloat(p->GetArg(9));
								 if( p->ArgInc(10) ) DB->TabPowerSupply->FieldValues["PSLowU"] = CIISStrToFloat(p->GetArg(10));
								 if( p->ArgInc(11) ) DB->TabPowerSupply->FieldValues["PSLowI"] = CIISStrToFloat(p->GetArg(11));
								 if( p->ArgInc(12) ) DB->TabPowerSupply->FieldValues["PSHighU"] = CIISStrToFloat(p->GetArg(12));
								 if( p->ArgInc(13) ) DB->TabPowerSupply->FieldValues["PSHighI"] = CIISStrToFloat(p->GetArg(13));

								 if( p->ArgInc(14) )
									 if( p->GetArg(14) == "True" )
									 {
										 DB->TabPowerSupply->FieldValues["PSAlarmEnabled"] = true;
										 DB->Log( ClientCom, AlarmEnable, High, p->GetArg(1), 0, 0 );
									 }
									 else
									 {
										 DB->TabPowerSupply->FieldValues["PSAlarmEnabled"] = false;
										 DB->TabPowerSupply->FieldValues["PSAlarmStatusU"] = 0;
										 DB->TabPowerSupply->FieldValues["PSAlarmStatusI"] = 0;
										 DB->Log( ClientCom, AlarmDisable, High, p->GetArg(1), 0, 0 );
										 NetInt->UpdateAlarms();
									 }
								 if( p->ArgInc(15) )
								 {
									 if( p->GetArg(15) == "True" )
									 {
										 DB->TabPowerSupply->FieldValues["PSVOutEnabled"] = true;
										 DB->Log( ClientCom, PSVOutEnable, High, p->GetArg(1), 0, 0 );
									 }
									 else
									 {
										 DB->TabPowerSupply->FieldValues["PSVOutEnabled"] = false;
										 DB->Log( ClientCom, PSVOutDisable, High, p->GetArg(1), 0, 0 );
									 }
								 }
								 if( p->ArgInc(16) )
								 {
									 if( p->GetArg(16) == "True" )
									 {
										 DB->TabPowerSupply->FieldValues["PSRemote"] = true;
										 DB->Log( ClientCom, PSRemote, High, p->GetArg(1), 0, 0 );
									 }
									 else
									 {
										 DB->TabPowerSupply->FieldValues["PSRemote"] = false;
										 DB->Log( ClientCom, PSLocal, High, p->GetArg(1), 0, 0 );
									 }
								 }


								 if( p->ArgInc(17) ) DB->TabPowerSupply->FieldValues["Ch1Gain"] = CIISStrToFloat(p->GetArg(17));
								 if( p->ArgInc(18) ) DB->TabPowerSupply->FieldValues["Ch1Offset"] = CIISStrToFloat(p->GetArg(18));
								 if( p->ArgInc(19) ) DB->TabPowerSupply->FieldValues["Ch1Unit"] = p->GetArg(19);
								 if( p->ArgInc(20) ) DB->TabPowerSupply->FieldValues["Ch2Gain"] = CIISStrToFloat(p->GetArg(20));
								 if( p->ArgInc(21) ) DB->TabPowerSupply->FieldValues["Ch2Offset"] = CIISStrToFloat(p->GetArg(21));
								 if( p->ArgInc(22) ) DB->TabPowerSupply->FieldValues["Ch2Unit"] = p->GetArg(22);
								 if( p->ArgInc(23) ) DB->TabPowerSupply->FieldValues["Ch3Gain"] = CIISStrToFloat(p->GetArg(23));
								 if( p->ArgInc(24) ) DB->TabPowerSupply->FieldValues["Ch3Offset"] = CIISStrToFloat(p->GetArg(24));
								 if( p->ArgInc(25) ) DB->TabPowerSupply->FieldValues["Ch3Unit"] = p->GetArg(25);
								 if( p->ArgInc(26) ) DB->TabPowerSupply->FieldValues["Ch4Gain"] = CIISStrToFloat(p->GetArg(26));
								 if( p->ArgInc(27) ) DB->TabPowerSupply->FieldValues["Ch4Offset"] = CIISStrToFloat(p->GetArg(27));
								 if( p->ArgInc(28) ) DB->TabPowerSupply->FieldValues["Ch4Unit"] = p->GetArg(28);
								 if( p->ArgInc(29) ) DB->TabPowerSupply->FieldValues["Ch1BitVal"] = CIISStrToFloat(p->GetArg(29));
								 if( p->ArgInc(30) ) DB->TabPowerSupply->FieldValues["Ch2BitVal"] = CIISStrToFloat(p->GetArg(30));
								 if( p->ArgInc(31) ) DB->TabPowerSupply->FieldValues["Ch3BitVal"] = CIISStrToFloat(p->GetArg(31));
								 if( p->ArgInc(32) ) DB->TabPowerSupply->FieldValues["Ch4BitVal"] = CIISStrToFloat(p->GetArg(32));

								 if( p->ArgInc(50) ) DB->TabPowerSupply->FieldValues["ZoneNo"] = StrToInt(p->GetArg(50));
								 DB->TabPowerSupply->Post();
								 o->MessageCode = CIISMsg_Ok;
								 break;
						 } // ( p->MessageTable )
					 }  // ModifyOK
					 else
					 {
						 o->MessageCode = CIISMsg_UserLevelError;
					 }
					 break;

				case 13: // Delete
					 if( ModifyOK )
					 {
						 switch( p->MessageTable )
						 {
						 case 10: // Project
								 if( DB->TabProject->Locate("PrjName",p->GetArg(1), NoOpts ) )
								 {
									 DB->TabProject->Delete();
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 20: // Controller
								 if( DB->TabControllers->Locate("CtrlName",p->GetArg(1), NoOpts ) )
								 {
									 DB->TabControllers->Delete();
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 30: // Sensors
								 if( DB->TabSensors->Locate("SensorSerialNo",StrToInt(p->GetArg(1)), NoOpts ) )
								 {
									 DB->TabSensors->Delete();
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 40: // Zones
								 if( DB->TabZones->Locate("ZoneNo",StrToInt(p->GetArg(1)), NoOpts ) )
								 {
									 DB->TabZones->Delete();
									 DB->Log( ClientCom, ZoneTabChanged, High, "", 0, 0 );
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 case 50: ;// Values
								 break;
						 case 60: // PowerSupply
								 if( DB->TabPowerSupply->Locate("PSName",p->GetArg(1), NoOpts ) )
								 {
									 DB->TabPowerSupply->Delete();
									 o->MessageCode = CIISMsg_Ok;
								 }
								 else
								 {
									 o->MessageCode = CIISMsg_RecNotFound;
								 }
								 break;
						 } // ( p->MessageTable )
					 }  // ModifyOK
					 else
					 {
						 o->MessageCode = CIISMsg_UserLevelError;
					 }
					 break;
	/*
					case CIIS:
						 o->MessageCode = CIISMsg_Ok;
						 o->MessageTable = 0;
						 o->MessageData = "1=" + DB->DBDir();
						 DB->Log( ClientCom, LogIn, High, p->GetArg(1), 0, 0 );
						 break;
	*/
					case CIISReqDBVer:
						 o->MessageCode = CIISMsg_Ok;
						 o->MessageTable = 0;
						 o->MessageData = "1=" + DB->TabProject->FieldByName("DBVer")->AsString;
					 break;

					case CIISReqPrgVer:
						 o->MessageCode = CIISMsg_Ok;
						 o->MessageTable = 0;
						 o->MessageData = "1=" + DB->TabProject->FieldByName("PrgVer")->AsString;
					 break;

					case CIISReqTime:
						 o->MessageCode = CIISMsg_Ok;
						 o->MessageTable = 0;
						 o->MessageData = "1=" + CIISDateTimeToStr(Now());
					 break;

					case CIISSetTime:
						o->MessageTable = 0;
						if( DB->RecRunning() ) o->MessageCode = 255;
						else
						{
							DB->TabProject->Edit();
							DB->TabProject->FieldValues["DTAdj"] = Now();
							DB->TabProject->Post();

							DB->Log( ClientCom, TimeSet, High, p->GetArg(1), 0, 0 );

							if( CIISSetSysTime(CIISStrToDateTime(p->GetArg(1))) ) o->MessageCode = CIISMsg_Ok;

							DB->TabProject->Edit();
							DB->TabProject->FieldValues["DTNew"] = Now();
							DB->TabProject->Post();

							

						}
					 break;


				case CIISLogout: // Logout
					 CurrSocket->UserLevel = ULNone;
					 CurrSocket->ClientLoggedIn = false;
					 o->MessageCode = CIISMsg_Ok;
					 o->MessageTable = 0;
					 DB->Log( ClientCom, LogOut, High, "", 0, 0 );
					 break;
				} // switch( p->MessageCommand )

			} // All commands exept CIISLogin
			else // ReadOK
			{
				o->MessageCode = CIISMsg_UserLevelError;
			}

		if( o->PortNo == PortNoLocal ) LSocket->PutMessage( o );
		else RSocket->PutMessage( o );

    delete p;
		}  // !(ServerMode == CIISRemoteServer )
		return true;
  }
else return false;  // No more messages in queue
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::SetDebugWin(TDebugWin * SetDebugWin)
{
  DW = SetDebugWin;
  LSocket->SetDebugWin( SetDebugWin );
  RSocket->SetDebugWin( SetDebugWin );
	PMSocket->SetDebugWin( SetDebugWin );
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::Debug(String Message)
{
  if( DW != NULL ) DW->Log(Message);
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabProjectRecord()
{
	String CDDate, Rec;

	try { CDDate = CIISDateTimeToStr( DB->TabProject->FieldByName("Commissioning")->AsDateTime ); }
  catch( ... ) { CDDate = ""; }

	Rec =    "1=" + DB->TabProject->FieldByName("PrjName")->AsString + "\r\n" +
           "2=" + DB->TabProject->FieldByName("PrjNo")->AsString + "\r\n" +
           "3=" + DB->TabProject->FieldByName("Client")->AsString + "\r\n" +
           "4=" + DB->TabProject->FieldByName("Consultant")->AsString + "\r\n" +
           "5=" + DB->TabProject->FieldByName("Manager")->AsString + "\r\n" +
           "6=" + DB->TabProject->FieldByName("ContractDescription")->AsString + "\r\n" +
           "7=" + DB->TabProject->FieldByName("MMResponsibility")->AsString + "\r\n" +
           "8=" + CDDate + "\r\n" +
           "9=" + DB->TabProject->FieldByName("Drawings")->AsString + "\r\n" +
           "10=" + DB->TabProject->FieldByName("Criteria")->AsString + "\r\n" +
           "11=" + DB->TabProject->FieldByName("ConnType")->AsString + "\r\n" +
           "12=" + DB->TabProject->FieldByName("ConnRemote")->AsString + "\r\n" +
           "13=" + DB->TabProject->FieldByName("ConnServerIP")->AsString + "\r\n" +
           "14=" + DB->TabProject->FieldByName("PrjAlarmStatus")->AsString + "\r\n"+
           "15=" + DB->TabProject->FieldByName("EventLogSize")->AsString + "\r\n" +
					 "16=" + DB->TabProject->FieldByName("EventLevel")->AsString + "\r\n" +
           "17=" + DB->TabProject->FieldByName("ServerStatus")->AsString + "\r\n" +
           "18=" + DB->TabProject->FieldByName("ServerMode")->AsString + "\r\n" +
           "19=" + DB->TabProject->FieldByName("ConfigVer")->AsString + "\r\n" +
					 "20=" + DB->TabProject->FieldByName("DataVer")->AsString + "\r\n" +
					 "21=" + DB->TabProject->FieldByName("LastValVer")->AsString + "\r\n" +
					 "22=" + DB->TabProject->FieldByName("DBVer")->AsString + "\r\n";
					 if( CurrSocket->UserLevel > ULNone ) Rec = Rec +
					 "24=" + DB->TabProject->FieldByName("PWReadOnly")->AsString + "\r\n";
					 if( CurrSocket->UserLevel > ULReadOnly ) Rec = Rec +
					 "25=" + DB->TabProject->FieldByName("PWModify")->AsString + "\r\n";
					 if( CurrSocket->UserLevel > ULModify ) Rec = Rec +
					 "26=" + DB->TabProject->FieldByName("PWErase")->AsString + "\r\n";

					 Rec = Rec +
					 "27=" + DB->TabProject->FieldByName("PrgVer")->AsString + "\r\n";
					 "28=" + CIISDateTimeToStr(DB->TabProject->FieldByName("DTAdj")->AsDateTime) + "\r\n" +
					 "29=" + CIISDateTimeToStr(DB->TabProject->FieldByName("DTNew")->AsDateTime) + "\r\n";

	return Rec;
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabControllersRecord()
{
  String SchDate;

	try { SchDate = CIISDateTimeToStr( DB->TabControllers->FieldByName("ScheduleDateTime")->AsDateTime ); }
  catch( ... ) { SchDate = ""; }

  return  "1=" + DB->TabControllers->FieldByName("CtrlName")->AsString + "\r\n" +
          "2=" + DB->TabControllers->FieldByName("NoIR")->AsString + "\r\n" +
          "3=" + SchDate + "\r\n" +
          "4=" + DB->TabControllers->FieldByName("SchedulePeriod")->AsString + "\r\n" +
          "5=" + DB->TabControllers->FieldByName("DecaySampInterval")->AsString + "\r\n" +
          "6=" + DB->TabControllers->FieldByName("DecayDuration")->AsString + "\r\n" +
          "7=" + DB->TabControllers->FieldByName("LPRRange")->AsString + "\r\n" +
          "8=" + DB->TabControllers->FieldByName("LPRStep")->AsString + "\r\n" +
          "9=" + DB->TabControllers->FieldByName("LPRDelay1")->AsString + "\r\n" +
          "10=" + DB->TabControllers->FieldByName("LPRDelay2")->AsString + "\r\n" +
          "11=" + DB->TabControllers->FieldByName("SampInterval1")->AsString + "\r\n" +
          "12=" + DB->TabControllers->FieldByName("SampInterval2")->AsString + "\r\n" +
          "13=" + DB->TabControllers->FieldByName("SampInterval3")->AsString + "\r\n" +
          "14=" + DB->TabControllers->FieldByName("SampInterval4")->AsString + "\r\n" +
          "15=" + DB->TabControllers->FieldByName("SampInterval5")->AsString + "\r\n" +
          "16=" + DB->TabControllers->FieldByName("NextCanAdr")->AsString + "\r\n" +
          "17=" + CIISFloatToStr( DB->TabControllers->FieldByName("DefaultSensorLow")->AsFloat ) + "\r\n" +
          "18=" + CIISFloatToStr( DB->TabControllers->FieldByName("DefaultSensorHigh")->AsFloat ) + "\r\n" +
          "19=" + DB->TabControllers->FieldByName("DefaultAlarmEnable")->AsString + "\r\n" +
          "20=" + DB->TabControllers->FieldByName("CtrlAlarmStatus")->AsString + "\r\n" +
          "21=" + DB->TabControllers->FieldByName("USBCANStatus")->AsString + "\r\n" +
          "22=" + DB->TabControllers->FieldByName("NodeCount")->AsString + "\r\n" +
          "23=" + DB->TabControllers->FieldByName("MonitorCount")->AsString + "\r\n" +
          "24=" + DB->TabControllers->FieldByName("DecayCount")->AsString + "\r\n" +
          "25=" + DB->TabControllers->FieldByName("LPRCount")->AsString + "\r\n" +
          "26=" + DB->TabControllers->FieldByName("SchedulePeriodUnit")->AsString + "\r\n" +
          "27=" + DB->TabControllers->FieldByName("ScheduleDecay")->AsString + "\r\n" +
          "28=" + DB->TabControllers->FieldByName("ScheduleLPR")->AsString + "\r\n" +
          "29=" + DB->TabControllers->FieldByName("CtrlScheduleStatus")->AsString + "\r\n" +
          "30=" + DB->TabControllers->FieldByName("LPRMode")->AsString + "\r\n" +
          "50=" + DB->TabControllers->FieldByName("PrjName")->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabZonesRecord()
{
  return  "1=" + DB->TabZones->FieldByName("ZoneNo")->AsString + "\r\n" +
          "2=" + DB->TabZones->FieldByName("ZoneName")->AsString + "\r\n" +
          "3=" + CIISFloatToStr( DB->TabZones->FieldByName("AnodeArea")->AsFloat ) + "\r\n" +
          "4=" + CIISFloatToStr( DB->TabZones->FieldByName("CathodeArea")->AsFloat ) +"\r\n" +
          "5=" + DB->TabZones->FieldByName("Comment")->AsString + "\r\n" +
          "6=" + DB->TabZones->FieldByName("ZoneSampInterval")->AsString + "\r\n" +
          "7=" + DB->TabZones->FieldByName("RecType")->AsString + "\r\n" +
          "8=" + DB->TabZones->FieldByName("RecNo")->AsString + "\r\n" +
          "9=" + DB->TabZones->FieldByName("ZoneCanAdr")->AsString + "\r\n" +
          "10=" + DB->TabZones->FieldByName("ZoneAlarmStatus")->AsString + "\r\n" +
          "11=" + DB->TabZones->FieldByName("ZoneScheduleStatus")->AsString + "\r\n" +
          "50=" + DB->TabZones->FieldByName("CtrlName")->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabSensorsRecord()
{
  return  "1=" + DB->TabSensors->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
          "2=" + DB->TabSensors->FieldByName( "SensorCanAdr" )->AsString + "\r\n" +
          "3=" + DB->TabSensors->FieldByName( "SensorStatus" )->AsString + "\r\n" +
          "4=" + DB->TabSensors->FieldByName( "SensorName" )->AsString + "\r\n" +
          "5=" + DB->TabSensors->FieldByName( "SensorType" )->AsString + "\r\n" +
          "6=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorLastValue")->AsFloat ) + "\r\n" +
          "7=" + CIISFloatToStr( DB->TabSensors->FieldByName("PreCommOff")->AsFloat ) + "\r\n" +
          "8=" + CIISFloatToStr( DB->TabSensors->FieldByName("PreCommLPR")->AsFloat ) + "\r\n" +
          "9=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorArea")->AsFloat ) + "\r\n" +
          "10=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorGain")->AsFloat ) + "\r\n" +
          "11=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorOffset")->AsFloat ) + "\r\n" +
          "12=" + DB->TabSensors->FieldByName( "SensorUnit" )->AsString + "\r\n" +
          "13=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorLow")->AsFloat ) + "\r\n" +
          "14=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorHigh")->AsFloat ) + "\r\n" +
          "15=" + DB->TabSensors->FieldByName( "SensorAlarmEnabled" )->AsString + "\r\n" +
          "16=" + DB->TabSensors->FieldByName( "SensorAlarmStatus" )->AsString + "\r\n" +
          "17=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorXPos")->AsFloat ) + "\r\n" +
          "18=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorYPos")->AsFloat ) + "\r\n" +
          "19=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorZPos")->AsFloat ) + "\r\n" +
          "20=" + DB->TabSensors->FieldByName( "SensorSectionNo" )->AsString + "\r\n" +
          "21=" + DB->TabSensors->FieldByName( "RequestStatus" )->AsString + "\r\n" +
          "22=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorIShunt")->AsFloat ) + "\r\n" +
          "23=" + DB->TabSensors->FieldByName( "SensorLPRStep" )->AsString + "\r\n" +
          "24=" + DB->TabSensors->FieldByName( "SensorConnected" )->AsString + "\r\n" +
          "25=" + DB->TabSensors->FieldByName( "SensorWarmUp" )->AsString + "\r\n" +
          "26=" + DB->TabSensors->FieldByName( "SensorTemp" )->AsString + "\r\n" +
          "27=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorLastTemp")->AsFloat ) + "\r\n" +
          "28=" + DB->TabSensors->FieldByName("SensorVerMajor")->AsString + "\r\n" +
		  "29=" + DB->TabSensors->FieldByName("SensorVerMinor")->AsString + "\r\n" +
		  "30=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorIShunt1")->AsFloat ) + "\r\n" +
		  "31=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorIShunt2")->AsFloat ) + "\r\n" +
		  "32=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorIShunt3")->AsFloat ) + "\r\n" +
          "50=" + DB->TabSensors->FieldByName( "ZoneNo" )->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabPowerSupplyRecord()
{
  return  "1=" + DB->TabPowerSupply->FieldByName( "PSSerialNo" )->AsString + "\r\n" +
          "2=" + DB->TabPowerSupply->FieldByName( "PSCanAdr" )->AsString + "\r\n" +
          "3=" + DB->TabPowerSupply->FieldByName( "PSStatus" )->AsString + "\r\n" +
          "4=" + DB->TabPowerSupply->FieldByName( "PSName" )->AsString + "\r\n" +
          "5=" + DB->TabPowerSupply->FieldByName( "PSSerialID" )->AsString + "\r\n" +
          "6=" + DB->TabPowerSupply->FieldByName( "PSType" )->AsString + "\r\n" +
          "7=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueU")->AsFloat ) + "\r\n" +
          "8=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueI")->AsFloat ) + "\r\n" +
          "9=" + DB->TabPowerSupply->FieldByName( "PSMode" )->AsString + "\r\n" +
          "10=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSSetVoltage")->AsFloat ) + "\r\n" +
          "11=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSSetCurrent")->AsFloat ) + "\r\n" +
          "12=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSOutGain")->AsFloat ) + "\r\n" +
          "13=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSOutOffset")->AsFloat ) + "\r\n" +
          "14=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLowU")->AsFloat ) + "\r\n" +
          "15=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLowI")->AsFloat ) + "\r\n" +
          "16=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSHighU")->AsFloat ) + "\r\n" +
          "17=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSHighI")->AsFloat ) + "\r\n" +
          "18=" + DB->TabPowerSupply->FieldByName( "PSAlarmEnabled" )->AsString + "\r\n" +
          "19=" + DB->TabPowerSupply->FieldByName( "PSAlarmStatusU" )->AsString + "\r\n" +
          "20=" + DB->TabPowerSupply->FieldByName( "PSAlarmStatusI" )->AsString + "\r\n" +
          "21=" + DB->TabPowerSupply->FieldByName( "PSVOutEnabled" )->AsString + "\r\n" +
          "22=" + DB->TabPowerSupply->FieldByName( "PSRemote" )->AsString + "\r\n" +
          "23=" + DB->TabPowerSupply->FieldByName( "RequestStatus" )->AsString + "\r\n" +
          "24=" + DB->TabPowerSupply->FieldByName( "PSConnected" )->AsString + "\r\n" +
          "25=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch1Gain")->AsFloat ) + "\r\n" +
          "26=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch1Offset")->AsFloat ) + "\r\n" +
          "27=" + DB->TabPowerSupply->FieldByName( "Ch1Unit" )->AsString + "\r\n" +
          "28=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch2Gain")->AsFloat ) + "\r\n" +
          "29=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch2Offset")->AsFloat ) + "\r\n" +
          "30=" + DB->TabPowerSupply->FieldByName( "Ch2Unit" )->AsString + "\r\n" +
          "31=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch3Gain")->AsFloat ) + "\r\n" +
          "32=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch3Offset")->AsFloat ) + "\r\n" +
          "33=" + DB->TabPowerSupply->FieldByName( "Ch3Unit" )->AsString + "\r\n" +
          "34=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch4Gain")->AsFloat ) + "\r\n" +
          "35=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch4Offset")->AsFloat ) + "\r\n" +
          "36=" + DB->TabPowerSupply->FieldByName( "Ch4Unit" )->AsString + "\r\n" +
          "37=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch1BitVal")->AsFloat ) + "\r\n" +
          "38=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch2BitVal")->AsFloat ) + "\r\n" +
          "39=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch3BitVal")->AsFloat ) + "\r\n" +
          "40=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("Ch4BitVal")->AsFloat ) + "\r\n" +
          "41=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueCh3")->AsFloat ) + "\r\n" +
          "42=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueCh4")->AsFloat ) + "\r\n" +
          "43=" + DB->TabPowerSupply->FieldByName( "PSVerMajor" )->AsString + "\r\n" +
          "44=" + DB->TabPowerSupply->FieldByName( "PSVerMinor" )->AsString + "\r\n" +
          "50=" + DB->TabPowerSupply->FieldByName( "ZoneNo" )->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabRecordingsRecord()
{
 String Stop;

 try { Stop = CIISDateTimeToStr( DB->TabRecordings->FieldByName("RecStop")->AsDateTime ); }
 catch( ... ) { Stop = ""; }


  return  "1=" + DB->TabRecordings->FieldByName( "RecNo" )->AsString + "\r\n" +
					"2=" + CIISDateTimeToStr(DB->TabRecordings->FieldByName("RecStart")->AsDateTime) + "\r\n" +
          "3=" + Stop + "\r\n" +
          "4=" + DB->TabRecordings->FieldByName( "RecType" )->AsString + "\r\n" +
          "5=" + DB->TabRecordings->FieldByName( "DecaySampInterval" )->AsString + "\r\n" +
          "6=" + DB->TabRecordings->FieldByName( "DecayDuration" )->AsString + "\r\n" +
          "7=" + DB->TabRecordings->FieldByName( "LPRRange" )->AsString + "\r\n" +
          "8=" + DB->TabRecordings->FieldByName( "LPRStep" )->AsString + "\r\n" +
          "9=" + DB->TabRecordings->FieldByName( "LPRDelay1" )->AsString + "\r\n" +
          "10=" + DB->TabRecordings->FieldByName( "LPRDelay2" )->AsString + "\r\n" +
          "11=" + DB->TabRecordings->FieldByName( "SampInterval" )->AsString + "\r\n" +
          "12=" + DB->TabRecordings->FieldByName( "LPRMode" )->AsString + "\r\n" +
          "50=" + DB->TabRecordings->FieldByName( "ZoneNo" )->AsString + "\r\n";


}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabEventLogRecord()
{
  return  "1=" + CIISDateTimeToStr(DB->TabEventLog->FieldByName("EventDateTime")->AsDateTime) + "\r\n" +
          "2=" + DB->TabEventLog->FieldByName("EventType")->AsString + "\r\n" +
          "3=" + DB->TabEventLog->FieldByName("EventCode")->AsString + "\r\n" +
          "4=" + DB->TabEventLog->FieldByName("EventLevel")->AsString + "\r\n" +
          "5=" + DB->TabEventLog->FieldByName("EventNo")->AsString + "\r\n" +
          "6=" + DB->TabEventLog->FieldByName("EventString")->AsString + "\r\n" +
          "7=" + DB->TabEventLog->FieldByName("EventInt")->AsString + "\r\n" +
					"8=" + DB->TabEventLog->FieldByName("EventFloat")->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabValuesRecord()
{
	return  "1=" + CIISDateTimeToStr(DB->TabValues->FieldByName("DateTimeStamp")->AsDateTime) + "\r\n" +
          "2=" + DB->TabValues->FieldByName( "RecNo" )->AsString + "\r\n" +
          "3=" + DB->TabValues->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
          "4=" + DB->TabValues->FieldByName( "ValueType" )->AsString + "\r\n" +
          "5=" + DB->TabValues->FieldByName( "ValueUnit" )->AsString + "\r\n" +
          "6=" + CIISFloatToStr( DB->TabValues->FieldByName("Value")->AsFloat ) + "\r\n" +
          "7=" + CIISFloatToStr( DB->TabValues->FieldByName("RawValue")->AsFloat ) + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabValuesLPRRecord()
{
	return  "1=" + CIISDateTimeToStr(DB->TabValuesLPR->FieldByName("DateTimeStamp")->AsDateTime) + "\r\n" +
					"2=" + DB->TabValuesLPR->FieldByName( "RecNo" )->AsString + "\r\n" +
					"3=" + DB->TabValuesLPR->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
					"4=" + CIISFloatToStr( DB->TabValuesLPR->FieldByName("ValueV")->AsFloat ) + "\r\n" +
          "5=" + CIISFloatToStr( DB->TabValuesLPR->FieldByName("ValueI")->AsFloat ) + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabValuesDecayRecord()
{
  return  "1=" + CIISDateTimeToStr(DB->TabValuesDecay->FieldByName("DateTimeStamp")->AsDateTime)+ "\r\n" +
		  "2=" + DB->TabValuesDecay->FieldByName( "RecNo" )->AsString + "\r\n" +
          "3=" + DB->TabValuesDecay->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
          "4=" + CIISFloatToStr( DB->TabValuesDecay->FieldByName("Value")->AsFloat ) + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabSensorsLastValRecord()
{
  return  "1=" + DB->TabSensors->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
          "6=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorLastValue")->AsFloat ) + "\r\n" +
          "27=" + CIISFloatToStr( DB->TabSensors->FieldByName("SensorLastTemp")->AsFloat ) + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabSensorsAlarmRecord()
{
  return  "1=" + DB->TabSensors->FieldByName( "SensorSerialNo" )->AsString + "\r\n" +
          "16=" + DB->TabSensors->FieldByName( "SensorAlarmStatus" )->AsString + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabPowerSupplyLastValRecord()
{
  return  "1=" + DB->TabPowerSupply->FieldByName( "PSSerialNo" )->AsString + "\r\n" +
          "7=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueU")->AsFloat ) + "\r\n" +
          "8=" + CIISFloatToStr( DB->TabPowerSupply->FieldByName("PSLastValueI")->AsFloat ) + "\r\n";
}
//---------------------------------------------------------------------------
String __fastcall TCIISClientInterface::TabPowerSupplyAlarmRecord()
{
  return  "1=" + DB->TabPowerSupply->FieldByName( "PSSerialNo" )->AsString + "\r\n" +
          "19=" + DB->TabPowerSupply->FieldByName( "PSAlarmStatusU" )->AsString + "\r\n" +
          "20=" + DB->TabPowerSupply->FieldByName( "PSAlarmStatusI" )->AsString + "\r\n";
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::GetLocalClientConnected()
{
  return LSocket->ClientConnected;
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::GetPMConnected()
{
	return PMSocket->ClientConnected;
}
//---------------------------------------------------------------------------
bool __fastcall TCIISClientInterface::GetRemoteClientConnected()
{
  return RSocket->ClientConnected;
}
//---------------------------------------------------------------------------
void __fastcall TCIISClientInterface::Tst1( String DBDir )
{
        LSocket->DisconnectClient();
        if( DB->SetDBDir( DBDir ))
        {
					LSocket->ConnectClient();
        }
}
//---------------------------------------------------------------------------


/*
									 }
									 else if( p->GetArg( 200 ) == "GetNextToEnd" &&
														p->ArgInc(1) && p->ArgInc(2) && p->ArgInc(3) )
									 {
										 RNo = StrToInt(p->GetArg(1));
										 Sensor = StrToInt(p->GetArg(2));
										 LPRStep = StrToInt(p->GetArg(3));

										 if( DB->TabRecordings->Locate("RecNo", RNo, NoOpts))
										 {
												RecStart = DB->TabRecordings->FieldByName("RecStart")->AsDateTime;
												if( DB->TabRecordings->FieldByName("LPRMode")->AsInteger == 0 )
													Interval = ( DB->TabRecordings->FieldByName("LPRDelay1")->AsInteger +
																			 DB->TabRecordings->FieldByName("LPRDelay2")->AsInteger ) * OneSecond;
												else
													Interval = OneSecond;

												DTS = RecStart + Interval * LPRStep;

												TVarRec Args;
												Args = DateTimeToStr( DTS );
												DB->TabValuesLPR->FindNearest( &Args, 0);

												while( DB->TabValuesLPR->FieldByName("RecNo")->AsInteger == RNo &&
															 DB->TabValuesLPR->FieldByName("SensorSerialNo")->AsInteger != Sensor &&
															 DB->TabValuesLPR->FindNext())
														
												if( DB->TabValuesLPR->FieldByName("SensorSerialNo")->AsInteger == Sensor )
												{
													o->MessageData = "";
													o->MessageCode = CIISMsg_Ok;
													while( o->MessageData.Length() < OutMessageLimit && DB->TabValuesLPR->FindNext() )
													{
													 o->MessageData = o->MessageData + TabValuesLPRRecord();
													}
												}
												else o->MessageCode = CIISMsg_RecNotFound;
										 }
										 else o->MessageCode = CIISMsg_RecNotFound;
									 }

*/
